UTL_HTTP making API call to fetch token , bad request

I am using utl_http for the first time, using documentation and online resources to build my code. I am trying to call an API that is returning a token. I have the API calls working using POSTMAN, but I am not able to get it working on PLSQL side using utl_http . I keep getting Bad request error or credential invalid error, but I know I have right credentials in my code same as POSTMAN.

Not able to figure what I am missing.

Here is the image from POSTMAN:

Headers

enter image description here

Body

enter image description here

Here is my code:

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;

I have validated the request against POSTMAN console raw output call request and matched it in PLSQL call request, but PLSQL side is always responding as Status code: 400 bad request error.

any idea what I am missing?



Comments

Popular posts from this blog

Today Walkin 14th-Sept

Spring Elasticsearch Operations

Hibernate Search - Elasticsearch with JSON manipulation