Question on setting a Token in HTTP Header

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
ronny45
Posts: 8
Joined: Wed Sep 24, 2025 7:52 pm

Question on setting a Token in HTTP Header

Post by ronny45 »

Hello,

I have used HTTP_REQ(POST) to get a token from a Token Endpoint URL using client id and secret. Now I need to call another API but this API needs the token value set in Header. Is there an example or suggestion on how to set the Header value?

Thank you
Scott Klement
Site Admin
Posts: 924
Joined: Sun Jul 04, 2021 5:12 am

Re: Question on setting a Token in HTTP Header

Post by Scott Klement »

If its the Authorization header (which is the most common) you can use the http_setAuth() procedure to set the authorization header.

If its any other header, you can use an exit proc (via http_xproc()) to allow you to add your own custom headers.

There are many examples of this in these forums, try searching for http_setauth and http_xproc
ronny45
Posts: 8
Joined: Wed Sep 24, 2025 7:52 pm

Re: Question on setting a Token in HTTP Header

Post by ronny45 »

Thank you so much for the reply Scott.

I am using HTTP_REQ and building req body to get the token back which is working fine. When I do the next HTTP_REQ call with request data, it is returning HTTP 404 Not Found. Same thing works fine in Postman. I am guessing that I am messing up the header somehow. Here is my code:

Code: Select all

http_debug(*on: '/home/C9C3T3/httpdebug.txt');            
http_setOption('timeout': '60');                          
http_setOption('network-ccsid': '1208');                  
http_setOption('content-type'                             
              :'application/json');              

http_xproc(HTTP_POINT_ADDL_HEADER: %paddr(AddHeaders)         
                                 : %addr(token_value));            

rc = http_req('POST'                                
                  :%trim(I_API_URL)                 
                  :*OMIT                            
                  :response                         
                  :*OMIT                            
                  :%trim(inputData)                   
                  :'application/json');                      
AddHeaders Procedure:

Code: Select all

dcl-proc AddHeaders;                                               
    dcl-pi *n varchar(32767) ccsid(1208);                          
       tokenPtr pointer value;                                     
    end-pi;                                                        
                                                                   
    dcl-c CRLF x'0D';                                              
    dcl-s headers varchar(32767) ccsid(1208) inz;                  
    dcl-s tokenStr varchar(1024);                                  
    tokenStr = %str(tokenPtr);                                     
                                                                   
    headers = 'Content-Type: application/json' + CRLF              
            + 'Authorization: Bearer ' + %trim(tokenStr) + CRLF;   
    return headers;                                                
end-proc;                                                          
ronny45
Posts: 8
Joined: Wed Sep 24, 2025 7:52 pm

Re: Question on setting a Token in HTTP Header

Post by ronny45 »

Nevermind Scott! it worked using

Code: Select all

http_setauth(HTTP_AUTH_BEARER  : ' ' : token);
You are amazing!!
ronny45
Posts: 8
Joined: Wed Sep 24, 2025 7:52 pm

Re: Question on setting a Token in HTTP Header

Post by ronny45 »

One more question - Do I need to reset anything before trying to retrieve the token again? I am noticing the HTTP_REQ comes back with error "This page requires a user-id & password" when I try to retrieve token again in same session. Once I log out and log-back in, the token retrieval works fine.

Code: Select all

 
http_debug(*on: '/home/httpdebug.txt');
http_setOption('timeout': '60');                                    
http_setOption('network-ccsid': '1208');                            
http_setOption( 'content-type'                                      
                      :'application/x-www-form-urlencoded' );       
                      
 formData =  'grant_type=client_credentials'                    
           + '&client_id='     + %trim(I_Client_ID)             
           + '&client_secret=' + %trim(I_Client_Secret);        
                                                                
 // HTTP_API Call to get Token in response which is Raw JSON    
 rc = http_req('POST'                                           
                   :%trim(I_API_URL)                            
                   :*OMIT                                       
                   :token
                   :*OMIT                                       
                   :%trim(formData)                             
                   :'application/x-www-form-urlencoded');       
Scott Klement
Site Admin
Posts: 924
Joined: Sun Jul 04, 2021 5:12 am

Re: Question on setting a Token in HTTP Header

Post by Scott Klement »

Are you sending authorization information using the http_setAuth() routine? If so, you need to remove it after you're done with it by calling http_setAuth() with HTTP_AUTH_NONE.

Are you sending additional headers with http_xproc()? If so, call it again after you are done and pass *NULL for the callback procedure to disable it.
ronny45
Posts: 8
Joined: Wed Sep 24, 2025 7:52 pm

Re: Question on setting a Token in HTTP Header

Post by ronny45 »

Yes thank you! that seems to have fixed the problem.
Post Reply