我正在从PL/SQL调用REST服务-在Oracle数据库12.2中使用UTL_HTTP进行HTTP POST请求,但我在执行函数"ORA-06502: PL/SQL:数字或值错误:字符到数字转换错误"后出现错误。
函数体:
declare
req utl_http.req;
res utl_http.resp;
url varchar2(4000) := 'http://localhost:9002/cinema';
host varchar2(4000) := 'test.com';
name varchar2(4000);
buffer varchar2(4000);
content varchar2(4000) := '{"room":"'||p_room_id||'", "partySize":"'||p_party_Size||'"}';
begin
req := utl_http.begin_request(url, 'POST',' HTTP/1.1', host);
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);
-- process the response from the HTTP call
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;
错误:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at line 11
06502. 00000 - "PL/SQL: numeric or value error%s"
*Cause: An arithmetic, numeric, string, conversion, or constraint error
occurred. For example, this error occurs if an attempt is made to
assign the value NULL to a variable declared NOT NULL, or if an
attempt is made to assign an integer larger than 99 to a variable
declared NUMBER(2).
*Action: Change the data, how it is manipulated, or how it is declared so
that values do not violate constraints.
这里发生错误
utl_http.begin_request(url, 'POST',' HTTP/1.1', host);
传递null
,而不是第三和第四个参数,这是默认值。试着看看这是否有效。
https://docs.oracle.com/database/121/ARPLS/u_http.htm ARPLS71021
看一下上面url中开始请求的例子。
****req := UTL_HTTP.BEGIN_REQUEST (url=>the_url, method=>'POST');****
UTL_HTTP.SET_HEADER (r => req,
name => 'Content-Type',
value => 'application/x-www-form-urlencoded');
UTL_HTTP.SET_HEADER (r => req,
name => 'Content-Length',
value =>' <length of data posted in bytes>');
UTL_HTTP.WRITE_TEXT (r => req,
data => 'p1 = value1&p2=value2...');
resp := UTL_HTTP.GET_RESPONSE
(r => req);