Oracle – PL/SQL simulate a form POST

databaseoracleplsqlstored-procedures

I'm writing an oracle procedure to make a post request to a certain URL. With this post request I have to send some form variables. In the API documentation they say they give the example in jquery+javascript. See:

var variables = {
   username: "username", 
   password: "password"
};
var jsonString = JSON.stringify(variables);
jQuery.post(
    "https://sandbox.api.cso20.net/v1/jobapi/getApiKey.json", 
    {args: jsonString}, 
    function (data) {console.log(data);}
);

I do know how to make http requests in PL SQL with utl_http and setting wallets for the https. But the only header I set at the moment is:

utl_http.set_header(r => l_req, name => 'User-Agent', value => 'Mozilla/4.0');

I understand the form info should somehow be sent with the post (header or body not sure). But I could really use a little bit help since I can't find any examples on the Internet. I did try this:

p_param_value = 'args={"username":"user","password":"pass"}';
l_param_length := length(p_param_value);
utl_http.set_header(r       =>   l_req,
                    name    =>   'Content-Length',
                    value   =>   l_param_length);

utl_http.write_text (r      =>   l_req,
                     data   =>   p_param_value);

But that unfortunately doesn't work. Any help is greatly appreciated.

Kevin

Best Solution

According to the utl_http documentation, you have to specify the POST method in begin_request:

req := UTL_HTTP.BEGIN_REQUEST (url=>the_url, method=>'POST');
UTL_HTTP.SET_HEADER (r      =>  req, 
                     name   =>  'Content-Type',   
                     value  =>  'application/x-www-form-urlencoded');
UTL_HTTP.SET_HEADER (r      =>   req, 
                     name   =>   'Content-Length', 
                     value  =>'  <length of data posted in bytes>');
UTL_HTTP.WRITE_TEXT (r      =>   req, 
                     data   =>   'p1 = value1&p2=value2...');
resp := UTL_HTTP.GET_RESPONSE (r => req);