Page 1 of 1

Question on setting a Token in HTTP Header

Posted: Mon Sep 29, 2025 3:29 pm
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

Re: Question on setting a Token in HTTP Header

Posted: Mon Sep 29, 2025 5:03 pm
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

Re: Question on setting a Token in HTTP Header

Posted: Thu Oct 02, 2025 9:24 pm
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;                                                          

Re: Question on setting a Token in HTTP Header

Posted: Thu Oct 02, 2025 9:44 pm
by ronny45
Nevermind Scott! it worked using

Code: Select all

http_setauth(HTTP_AUTH_BEARER  : ' ' : token);
You are amazing!!

Re: Question on setting a Token in HTTP Header

Posted: Fri Oct 03, 2025 3:30 pm
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');       

Re: Question on setting a Token in HTTP Header

Posted: Fri Oct 03, 2025 9:03 pm
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.

Re: Question on setting a Token in HTTP Header

Posted: Mon Oct 06, 2025 3:29 pm
by ronny45
Yes thank you! that seems to have fixed the problem.