如何在Oracle PL / SQL中传递发布请求参数

时间:2020-09-18 15:47:17

标签: oracle plsql oracle18c

我有一个cURL命令,如下所示:

curl --insecure -X POST https://www.example.com -H 'accept-encoding: gzip,deflate' -H 'cache-control: no-cache' -H 'content-type: application/x-www-form-urlencoded' --data "request_type=secure&scope=global&user_id=temp&password=temppass"

我不确定,如何从PL / SQL过程中调用此POST请求。

尤其是我不知道如何传递--data参数。

DECLARE
    http_request    UTL_HTTP.req;
    http_response   UTL_HTTP.resp;
    return_text VARCHAR2(2000);
BEGIN
    http_request := UTL_HTTP.begin_request('https://www.example.com/path/sub_path');

    UTL_HTTP.set_header(http_request, 'accept-encoding', 'gzip,deflate');
    UTL_HTTP.set_header(http_request, 'cache-control', 'no-cache');
    UTL_HTTP.set_header(http_request, 'content-type', 'application/x-www-form-urlencoded');

    UTL_HTTP.set_authentication(http_request, 'temp', 'temppass');

    http_response := UTL_HTTP.get_response(http_request);

    UTL_HTTP.read_text(http_response, return_text);
    dbms_output.put_line (return_text);
END;
/

请有人帮我吗?预先感谢!

1 个答案:

答案 0 :(得分:2)

在构建请求的第一条语句中,声明HTTP方法。

http_request := UTL_HTTP.begin_request('https://www.example.com/path/sub_path','POST');

可以找到有关如何使用正文发送请求的示例here

代码示例:

DECLARE
    http_request    UTL_HTTP.req;
    http_response   UTL_HTTP.resp;
    return_text     VARCHAR2 (2000);
BEGIN
    http_request := UTL_HTTP.begin_request ('https://www.example.com/path/sub_path', 'POST');

    UTL_HTTP.set_header (http_request, 'accept-encoding', 'gzip,deflate');
    UTL_HTTP.set_header (http_request, 'cache-control', 'no-cache');
    UTL_HTTP.set_header (http_request, 'content-type', 'application/x-www-form-urlencoded');

    UTL_HTTP.set_authentication (http_request, 'temp', 'temppass');

    UTL_HTTP.write_text (http_request,
                         'request_type=secure&scope=global&user_id=temp&password=temppass');

    http_response := UTL_HTTP.get_response (http_request);

    UTL_HTTP.read_text (http_response, return_text);
    DBMS_OUTPUT.put_line (return_text);
END;
/