如何在PL / SQL中发送带有表单数据和参数的POST请求 [英] How to sent a POST request with form-data and parameters in PL/SQL

查看:234
本文介绍了如何在PL / SQL中发送带有表单数据和参数的POST请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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


内容类型必须是multipart / form-data

以下是我所拥有的:

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;

以下是CURL的示例:

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

此示例适用于curl,但我不知道为什么它不适用于PL / SQL。
你能帮帮我吗?

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

推荐答案

我认为你不能像在URL中那样使用带有GET查询的POST ...

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

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

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


还有同样的问题在这里开发,包含大量代码。

希望有所帮助。

这篇关于如何在PL / SQL中发送带有表单数据和参数的POST请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆