如何在plsql中将POST请求作为json发送

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24838138/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:25:58  来源:igfitidea点击:

How to sent a POST request as json in plsql

jsonpostplsqlhttp-posthttprequest

提问by prince

I have the following basic code which i use to post a plsql json request. The webservice getting executed doesnt have any response as it is simply for carrying out a certain task. But each time i execute the block, i get the status code 400 from Apache Tomcat. Where is it that i am going wrong?

我有以下基本代码,用于发布 plsql json 请求。正在执行的 web 服务没有任何响应,因为它只是为了执行某个任务。但是每次执行该块时,我都会从 Apache Tomcat 获得状态代码 400。我哪里出错了?

declare
  http_resp utl_http.resp;
  http_req utl_http.req;
  json_msg VARCHAR2(500);
begin

  http_req := utl_http.begin_request('http://192.168.1.194:8080/NotificationApp/sendNotification.rest', 'POST');
  utl_http.set_body_charset(http_req, 'UTF-8');
  utl_http.set_header(http_req, 'Content-Type', 'application/json');

    json_msg := '{"code":100,"id": "APA91bFSmD_gBsUwP_hraRZL20mt8p4ejGn5fC7tlciINT50Ad8oIod2T-64GVk_8rrjoqXGEpYuRcoQogG0L7aOKIjeeisTcmHiUUONbnZzn4_u0ED7QD_iNeVkh2RU8Pa-HBHwgJUgOT-TyvlM9hB4Yn9fvWER","data": "alert alert"}';

  utl_http.write_text(http_req, dbms_lob.substr(json_msg,dbms_lob.getLength(json_msg),1));

 http_resp := utl_http.get_response(http_req);

  if (http_resp.status_code >= 400) and
            (http_resp.status_code <= 499)
        then
        dbms_output.put_line(http_resp.status_code);

        end if;

  utl_http.end_response(http_resp);

end;

Thanks in advance

提前致谢

回答by prince

After a lot of searching, i got the following code from a blog. Works fine for me.

经过大量搜索,我从博客中获得了以下代码。对我来说很好用。

declare
req utl_http.req;
res utl_http.resp;
url varchar2(4000) := 'http://192.168.1.194:8080/NotificationApp/sendNotification.rest';
name varchar2(4000);
buffer varchar2(4000); 
content varchar2(4000) := '{"code":100,"id": "APA91bFSmD_gBsUwO_hraRZL20mt8p4ejGn5fC7tlciINT50Ad8oIod2T-64GVk_8rProqXGEpYuDcoQogG0L7a0TuyeeisTcmHiUUONbnZzn4_u0ED7QD_iNeVkh1ZgU8Pa-HRtfgJUgOT-TyvlM9hB4Yn9fvOPud","data": "alert alert"}';

begin

req := utl_http.begin_request(url, 'POST',' HTTP/1.1');
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));

utl_http.write_text(req, content);

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

回答by Mayara Leidh

Now, I needed to change the line req := utl_http.begin_request(url, 'POST',' HTTP/1.1');

现在,我需要更改行 req := utl_http.begin_request(url, 'POST',' HTTP/1.1');

I did and worked: req := utl_http.begin_request(url, 'POST');

我做了并工作: req := utl_http.begin_request(url, 'POST');