我第一次使用utl_http
,使用文档和在线资源来构建我的代码。我试图调用一个返回令牌的API。我使用POSTMAN进行了API调用,但我无法使用utl_http在PLSQL端进行调用。我一直收到Bad request error。
不知道我错过了什么。
下面是我的代码:
declare
req utl_http.req;
res utl_http.resp;
l_lvc_content varchar2(4000);
buffer varchar2(4000);
endLoop boolean;
begin
-- making request
begin
--utl_http.set_persistent_conn_support(true, 30);
utl_http.set_transfer_timeout(15);
utl_http.set_detailed_excp_support(true);
utl_http.set_wallet('file:/mywallet/wallet', 'MywalletPASS');
req := utl_http.begin_request('https://myurl.com/api/oauth2/token', 'POST');
utl_http.set_header(req, 'Authorization', 'Basic QzkzZ0N6amVCbGlaNWlXdEF1dUVnemasaZFcEFpMXdzTE46TXFvdWxpcW85UExBbjM2Ug==');
utl_http.set_header(req, 'Content-Type', 'application/x-www-form-urlencoded');
l_lvc_content := 'grant_type=password&username=MyUserNAME&password=MyUserPASS#&channel=Mychannel';
utl_http.set_header(req, 'Content-Length', nvl(length(l_lvc_content),0) );
utl_http.write_text(req, l_lvc_content);
res := utl_http.get_response(req);
exception
when utl_http.request_failed
then dbms_output.put_line('ERROR : Request Failed : ' || utl_http.get_detailed_sqlerrm );
utl_http.end_response(res);
when others
then dbms_output.put_line('RESPONSE ERROR' || SQLERRM);
utl_http.end_response(res);
end;
dbms_output.put_line('RESPONSE Received');
-- process the response from the HTTP call
begin
dbms_output.put_line('Reading the RESPONSE');
dbms_output.put_line ('Status code: ' || res.status_code);
dbms_output.put_line ('Reason : ' || res.reason_phrase);
loop
exit when endLoop;
begin
utl_http.read_line( res, buffer, true );
if (buffer is not null) and length(buffer)>0 then
dbms_output.put_line(buffer);
end if;
exception when utl_http.END_OF_BODY then
endLoop := true;
end;
end loop;
utl_http.end_response(res);
dbms_output.put_line('RESPONSE read complete');
exception
when utl_http.end_of_body
then utl_http.end_response(res);
when others
then dbms_output.put_line('RESPONSE ERROR' || SQLERRM);
utl_http.end_response(res);
end;
exception
when others
then dbms_output.put_line('MAIN ERROR : '|| SQLERRM);
utl_http.end_response(res);
end;
字符串
我已经根据POSTMAN控制台原始输出调用请求验证了请求,并将其与PLSQL调用请求相匹配,但PLSQL端总是响应状态码:400 bad request error。
知道我错过了什么吗
1条答案
按热度按时间jm81lzqq1#
我得到了它的工作经过几次尝试.这里是最后的工作代码.
字符串
开始
型
exception when others then dbms_output.put_line('MAIN ERROR:'||SQL(); utl_http.end_response(res);
结束;