How to sent a POST request with form-data and parameters in PL/SQL

流过昼夜 提交于 2019-12-29 09:19:12

问题


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 ?


回答1:


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.




回答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



来源:https://stackoverflow.com/questions/44393679/how-to-sent-a-post-request-with-form-data-and-parameters-in-pl-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!