如何在 PL/SQL 中发送带有表单数据和参数的 POST 请求

我正在尝试在 PL/SQL 中调用 REST WebService,但它不起作用.我收到此错误:

<块引用>

Content-type 必须是 multipart/form-data

这是我所拥有的:

DECLARE请求 UTL_HTTP.REQ;响应 UTL_HTTP.RESP;值 VARCHAR2(1024);-- 发布到的网址v_url VARCHAR2(200) := 'http://local/api/ws';-- 发布参数v_param VARCHAR2(500) := 'art=11111&qty=1';v_param_length NUMBER := length(v_param);开始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,名称 =>'内容类型',值 =>'多部分/表单数据;字符集=utf-8;边界=/');UTL_HTTP.SET_HEADER (r => req,名称 =>'内容长度',值 =>v_param_length);UTL_HTTP.WRITE_TEXT (r => req,数据 =>v_param);响应:= UTL_HTTP.GET_RESPONSE(req);环形UTL_HTTP.READ_LINE(resp, value, TRUE);DBMS_OUTPUT.PUT_LINE(值);结束循环;UTL_HTTP.END_RESPONSE(resp);例外WHEN UTL_HTTP.END_OF_BODY THENUTL_HTTP.END_RESPONSE(resp);结尾;

这是一个带有 CURL 的例子:

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

这个例子在 curl 上工作得很好,但我不知道为什么它在 PL/SQL 中没有.你能帮助我吗 ?

解决方案

我认为您不能像在 URL 中那样使用 POST 和 GET 查询...

做你期望的事情看起来很困难.以下是来自 Ask Tom 的一些输入:

这个想法是生成帖子文本,包括一个特定的边界",看起来像这样:

<块引用>

POST/path/to/script.php HTTP/1.0主机:example.com内容类型:多部分/表单数据,边界=AaB03x内容长度:$requestlen--AaB03x内容配置:表单数据;名称=字段1"$field1--AaB03x内容配置:表单数据;名称=字段2"$field2--AaB03x内容配置:表单数据;名称=用户文件";文件名="$文件名"内容类型:$mimetype内容传输编码:二进制$二进制数据--AaB03x--

还有 相同问题在这里开发有很多代码.

希望有帮助.

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 ?

解决方案

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.

相关文章