oracle UTL_HTTP发出API调用以获取令牌,错误请求

3phpmpom  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(174)

我第一次使用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。
知道我错过了什么吗

jm81lzqq

jm81lzqq1#

我得到了它的工作经过几次尝试.这里是最后的工作代码.

declare
req utl_http.req;
res utl_http.resp;

buffer              varchar2(4000); 
endLoop             boolean; 

lvc_wallet_path     varchar2(100):= 'file:/mywallet/wallet';
lvc_wallet_pass     varchar2(20):=  'MyWalletPASS';
    
lvc_username        varchar2(20):= 'MyUserNAME' ;
lvc_pwd             varchar2(20):= 'MyUserPASS#' ;

lvc_auth_url        varchar2(100):= 'https://myurl.com/api/oauth2/token';
    
lvc_req_typ_post    varchar2(10):= 'POST';
lvc_autorization    varchar2(100):= 'Basic QzkzZ0N6amVCbGlaNWlXdEF1dUVnemZFcEFpMXdzTE46TXFvdWxpcW85UExBbjM2Ug==';
lvc_encoding        varchar2(20):=  'gzip, deflate, br';        
lvc_content_type    varchar2(100):= 'application/x-www-form-urlencoded'; 
lvc_content         varchar2(100):= 'grant_type=password' ||'&'|| 'username='||lvc_username ||'&'|| 'password='||lvc_pwd ||'&'|| 'channel=oak';

字符串
开始

-- making request
begin

    --utl_http.set_persistent_conn_support(true, 30);
    
    utl_http.set_transfer_timeout(15);       
    utl_http.set_response_error_check(true);
    utl_http.set_detailed_excp_support(true);

    utl_http.set_wallet(lvc_wallet_path, lvc_wallet_pass);
        
    req := utl_http.begin_request(lvc_auth_url, lvc_req_typ_post);
           
    utl_http.set_header(req, 'Authorization', lvc_autorization);
           
    utl_http.set_header(req, 'Accept-Encoding', lvc_encoding);
             
    utl_http.set_header(req, 'Content-Type', lvc_content_type);
                   
    utl_http.set_header(req, 'Content-Length', length(lvc_content)) ;       
    
    utl_http.write_text(req,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:'||SQL(); utl_http.end_response(res);
结束;

相关问题