如何在 PL/SQL 中发送带有表单数据和参数的 POST 请求
How to sent a POST request with form-data and parameters in PL/SQL
我试图在 PL/SQL 中调用 REST WebService,但它不起作用。我收到此错误:
Content-type must be 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 示例:
curl -v -X POST -H "Content-Type: multipart/form-data" -F "art=11111" -F "qty=1" http://local/api/ws
这个例子在 curl 上运行良好,但我不知道为什么它在 PL/SQL 中不起作用。
你能帮助我吗 ?
我在 http 请求中使用 POST 方法的类似过程,但是我将 header 设置为:
UTL_HTTP.set_header( v_http_request,
'Content-Type',
'application/x-www-form-urlencoded;charset=utf-8' );
另请注意,如果 web-service 站点实施安全证书,您的 DBA 必须创建一个钱包 server-side,并且在打开请求之前您应该调用该钱包:
utl_http.set_wallet('file:<your wallet file route>', '<your pass>');
可以找到有关钱包的更多信息here
我认为您不能像在 URL...
中那样使用带有 GET 查询的 POST
看起来很难做到你所期望的。这是来自 Ask Tom 的一些输入:
这个想法是生成 post 文本,包括一个特定的 "boundary",它看起来像这样:
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--
这里还有same issue developed,里面有很多代码。
希望对您有所帮助。
我试图在 PL/SQL 中调用 REST WebService,但它不起作用。我收到此错误:
Content-type must be 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 示例:
curl -v -X POST -H "Content-Type: multipart/form-data" -F "art=11111" -F "qty=1" http://local/api/ws
这个例子在 curl 上运行良好,但我不知道为什么它在 PL/SQL 中不起作用。 你能帮助我吗 ?
我在 http 请求中使用 POST 方法的类似过程,但是我将 header 设置为:
UTL_HTTP.set_header( v_http_request,
'Content-Type',
'application/x-www-form-urlencoded;charset=utf-8' );
另请注意,如果 web-service 站点实施安全证书,您的 DBA 必须创建一个钱包 server-side,并且在打开请求之前您应该调用该钱包:
utl_http.set_wallet('file:<your wallet file route>', '<your pass>');
可以找到有关钱包的更多信息here
我认为您不能像在 URL...
中那样使用带有 GET 查询的 POST看起来很难做到你所期望的。这是来自 Ask Tom 的一些输入:
这个想法是生成 post 文本,包括一个特定的 "boundary",它看起来像这样:
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--
这里还有same issue developed,里面有很多代码。
希望对您有所帮助。