HTTP_POINT_ADDL_HEADER Header parameters

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
twisteddisco
Posts: 5
Joined: Thu Dec 12, 2024 9:05 pm

HTTP_POINT_ADDL_HEADER Header parameters

Post by twisteddisco »

Hello All,

Question on the HTTP_POINT_ADDL_HEADER routine.

I have a requirement where I need to create a webservice which will include HEADER parameters and a REQUEST body.

The HEADER parameters are:
IvUserKey
ivUserBic

The Request Body parameters are:
party
partyAccount

I have never used HTTPAPI to add header parameters, looking at existing posts I think I need to do something like this using

Code: Select all

   
// Notify httpapi of the routine to use to add headers
http_xproc( HTTP_POINT_ADDL_HEADER: %paddr(AddHeaders));

// --------------------------------------------------                    
// Perform the http request and get back the response                    
// --------------------------------------------------                    
Monitor;                                                                 
wResponse = http_string('POST': wUrl: eJsonString: 'application/json'); 

dcl-proc AddHeaders;
  dcl-pi *n;
    toBeAdded varchar(32767);
  end-pi;

  toBeAdded = 'IvUserKey: valuea' + x'0d25'
     		   + 'ivUserBic: valueb' + x'0d25';
 end-proc;
 
Is this the correct way to go about this?

Thankyou for your time, much appreciated
Scott Klement
Site Admin
Posts: 906
Joined: Sun Jul 04, 2021 5:12 am

Re: HTTP_POINT_ADDL_HEADER Header parameters

Post by Scott Klement »

It looks correct to me.
sathis_nsk
Posts: 1
Joined: Fri May 03, 2024 10:45 am

Re: HTTP_POINT_ADDL_HEADER Header parameters

Post by sathis_nsk »

Just an additional info.

I used the following statement to register a header handler:

Code: Select all

http_xproc( HTTP_POINT_ADDL_HEADER  : %paddr(Add_headers) ); 
However, I noticed that subsequent HTTP requests to different endpoints (such as the authentication token request) within the same program continued to carry over the same headers.

To resolve this, I had to explicitly deregister the header handler after each HTTP request by using:

Code: Select all

http_xproc( HTTP_POINT_ADDL_HEADER  : *NULL );                
Scott Klement
Site Admin
Posts: 906
Joined: Sun Jul 04, 2021 5:12 am

Re: HTTP_POINT_ADDL_HEADER Header parameters

Post by Scott Klement »

Yes, that is the expected behavior.
twisteddisco
Posts: 5
Joined: Thu Dec 12, 2024 9:05 pm

Re: HTTP_POINT_ADDL_HEADER Header parameters

Post by twisteddisco »

It seems my authorisation header needs to be in BASE64, for this I have downloaded Scott's BASE64R4 program.

The program BASE64R4 makes perfect sense (Thanks Scott) but I just wondered about the format of the data when I add it to header, once converted does it still need the CRLF at the end?

For example:
My details are:

User Id: demo
Password: p@55w0rd

When encoded to BASE64 this looks like ZGVtbzpwQDU1dzByZA==

The format of the authorization header should be Authorization: Basic ZGVtbzpwQDU1dzByZA==
Scott Klement
Site Admin
Posts: 906
Joined: Sun Jul 04, 2021 5:12 am

Re: HTTP_POINT_ADDL_HEADER Header parameters

Post by Scott Klement »

Support for that is built-in. Just do this before your request:

Code: Select all

http_setauth(http_auth_basic : 'demo' : 'p@55w0rd' );
...http_string...and other code here...

After you are done, remove the password from memory...

Code: Select all

http_setauth(http_auth_none:'':'');
Post Reply