gpt4 book ai didi

json - 如何在plsql中以json形式发送POST请求

转载 作者:行者123 更新时间:2023-12-01 18:57:47 31 4
gpt4 key购买 nike

我有以下基本代码,我用它来发布 plsql json 请求。执行的Web服务没有任何响应,因为它只是为了执行某个任务。但每次执行该 block 时,我都会从 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;

提前致谢

最佳答案

经过大量搜索,我从 blog 得到了以下代码。对我来说效果很好。

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;

关于json - 如何在plsql中以json形式发送POST请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24838138/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com