【发布时间】:2019-11-11 13:29:27
【问题描述】:
我正在尝试从 plsql 调用 REST Web 服务,使用 POST 方法,并尝试将参数作为查询字符串中的 json 发送。不幸的是,我失败可能是因为我没有正确使用 utl_http(这里是 plsql 新手)。
恐怕我没有正确发送请求并且我没有在查询字符串中提交 json。
实际上,Web 服务的提供者期望这样的东西:BASE URL/postPayment/jsonstring,而我恐怕并没有这样做。
实际上我收到一个 HTTP 405 错误:“指定的 HTTP 方法不允许用于请求的资源 ()。”
我试图四处搜索,发送 json 的最佳方式是什么(最初我尝试使用 APEX_WEB_SERVICE.MAKE_REST_REQUEST 但返回的结果为空)但似乎我找不到关于我的情况的进一步指导。
有人可以通过在我的案例中指出正确的 uttl_http 使用形式,或者通过在下面指出我的错误来指导我应该如何进行?
create or replace procedure another_way
(
customerNo IN VARCHAR2,
invoiceNo IN VARCHAR2,
agreementNo IN VARCHAR2,
instance IN VARCHAR2,
paymentRefId IN VARCHAR2,
paymentDate IN VARCHAR2,
principalAmt IN VARCHAR2,
interest IN VARCHAR2,
totalAmt IN VARCHAR2,
passCode IN VARCHAR2,
invoiceType IN VARCHAR2,
phoneNo IN VARCHAR2
) is
req utl_http.req;
res utl_http.resp;
url varchar2(4000) := 'http://baseurl:7474/mpower/rest/postPayment/';
name varchar2(4000);
buffer varchar2(4000);
content varchar2(4000) := '{"customerNo":"'||customerNo||'","invoiceNo":"'||invoiceNo||'","agreementNo":"","instance":"'||instance||'","paymentRefId":"'||paymentRefId||'","paymentDate":"'||paymentDate||'","principalAmt":'||principalAmt||',"interest":'||interest||',"totalAmt":'||totalAmt||',"passCode":"'||passCode||'","invoiceType":"'||invoiceType||'","phoneNo":"'||phoneNo||'"}';
begin
/* just to indicate it started */
dbms_output.put_line('START');
/* to check if your JSON content is okay and has correct values */
dbms_output.put_line(content);
dbms_output.put_line(url||content);
req := utl_http.begin_request(url||content, 'POST',' HTTP/1.1');
utl_http.set_authentication( req, 'abi','oshee', 'Basic' );
--utl_http.set_header(req, 'user-agent', 'mozilla/4.0');
--utl_http.set_header(req, 'content-type', 'application/json');
--utl_http.set_header(req, 'Content-Length', length(content));
/* not sure if this will work but try to print the utl_http */
res := utl_http.get_response(req);
begin
loop
utl_http.read_line(res, buffer);
dbms_output.put_line(buffer);
end loop;
utl_http.end_response(res);
exception
when utl_http.end_of_body then
utl_http.end_response(res);
end;
/* just to indicate it ended */
dbms_output.put_line('END');
exception
when utl_http.end_of_body
then
utl_http.end_response(res);
/* just to indicate it went to exception part */
DBMS_OUTPUT.put_line (SQLERRM);
dbms_output.put_line('EXCEPTION');
end;
--end another_way;
【问题讨论】:
-
您应该返回使用 APEX_WEB_SERVICE.MAKE_REST_REQUEST(它也可以进行基本身份验证)。你说你没有得到响应,但你至少得到了 HTTP 状态码吗?如果是这样,它是什么?接下来,为什么要在查询字符串中发送数据?使用 REST 发布时,通常使用 HTTP 请求的主体来发送数据。最后,如果你让它在 Postman 中工作,请执行以下操作:点击右侧的 Code 链接,选择 cURL,然后点击 Copy to Clipboard乙>。删除所有敏感信息并向我们展示其中的内容。
-
实际上提供商已经建立了规则:因此我们有义务将 json 作为查询字符串发送。据我所知,REST 并没有强制要求在正文中发送参数。然而,你是对的。我回到使用 APEX_WEB_SERVICE.MAKE_REST_REQUEST 但通过使用 uttl_url.escape 引入了 url 编码的缺失部分!!!!现在我可以得到回应了!!!! :) :) :) 再次感谢您。
标签: json plsql query-string oracle-apex invoke-webrequest