2

I'm trying to call REST WebService in PL/SQL but it doesn't work. I get this error:

Content-type must be multipart/form-data

Here is what I have:

DECLARE
  req   UTL_HTTP.REQ;
  resp  UTL_HTTP.RESP;
  value VARCHAR2(1024);  -- URL to post to
  v_url VARCHAR2(200) := 'http://local/api/ws';
  -- Post Parameters
  v_param VARCHAR2(500) := 'art=11111\&qty=1'; 
  v_param_length NUMBER := length(v_param);
BEGIN
  req := UTL_HTTP.BEGIN_REQUEST (url=> v_url, method => 'POST');
  UTL_HTTP.SET_HEADER(req, 'User-Agent', 'Mozilla/4.0');
  UTL_HTTP.SET_HEADER (r      =>  req,
                       name   =>  'Content-Type',
                       value  =>  'multipart/form-data; charset=utf-8; boundary=/');
  UTL_HTTP.SET_HEADER (r      =>   req,
                       name   =>   'Content-Length',
                       value  =>   v_param_length);
  UTL_HTTP.WRITE_TEXT (r      =>   req,
                       data   =>   v_param); 

     resp := UTL_HTTP.GET_RESPONSE(req);
  LOOP
    UTL_HTTP.READ_LINE(resp, value, TRUE);
    DBMS_OUTPUT.PUT_LINE(value);
  END LOOP;
  UTL_HTTP.END_RESPONSE(resp);
EXCEPTION
  WHEN UTL_HTTP.END_OF_BODY THEN
    UTL_HTTP.END_RESPONSE(resp);
END;

Here is an example with CURL :

curl -v -X POST -H "Content-Type: multipart/form-data" -F "art=11111" -F "qty=1" http://local/api/ws

This example works fine with curl but i don't know why it doesn't in PL/SQL. Can you help me ?

J. Chomel
  • 8,193
  • 15
  • 41
  • 69
Sebus
  • 47
  • 1
  • 1
  • 10
  • Related, but no solution: https://stackoverflow.com/questions/23925278/oracle-utl-http-post-encoding-multipart-form-data – J. Chomel Jun 07 '17 at 09:21

2 Answers2

2

I use a similar procedure with POST method at the http request, however I'd set header like:

UTL_HTTP.set_header( v_http_request, 
                     'Content-Type', 
                     'application/x-www-form-urlencoded;charset=utf-8' );

Be aware also that, if the web-service site implements a security certificate, your DBA must create a wallet server-side, and before opening the request you should invoke that wallet:

utl_http.set_wallet('file:<your wallet file route>', '<your pass>');

More on Wallets can be found here

Kjartan
  • 18,591
  • 15
  • 71
  • 96
Jair Hernandez
  • 494
  • 3
  • 7
2

I don't think you can use a POST with GET query like you do in the URL...

It looks quite difficult to do what you expect. Here is some input from Ask Tom:

The idea is to generate the post text, including a specific "boundary", which will look like this:

POST /path/to/script.php HTTP/1.0
Host: example.com
Content-type: multipart/form-data, boundary=AaB03x
Content-Length: $requestlen

--AaB03x
content-disposition: form-data; name="field1"

$field1
--AaB03x
content-disposition: form-data; name="field2"

$field2
--AaB03x
content-disposition: form-data; name="userfile"; filename="$filename"
Content-Type: $mimetype
Content-Transfer-Encoding: binary

$binarydata
--AaB03x--

There is also same issue developed here with lots of code.

Hope it helps.

J. Chomel
  • 8,193
  • 15
  • 41
  • 69