HTTP header

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
Giel
Posts: 3
Joined: Tue Nov 21, 2023 4:40 am

HTTP header

Post by Giel »

Good day, I need to add the following http header for my soap call, I am working on RPGLE.
The addition is C# is : request.Headers.Add("Identity", "root");
This is what my SOAP looks like currently:

Out_Http_Err = *Blank;
http_setOption('content-type': 'application/soap+xml; charset=utf-8');
SoapAction = '"http://tempuri.org/IDMS/GetDoc"';
url = 'http://dms.qa.abgza.co.za/API/DMS.svc?singleWsdl';

SOAP = '<?xml version="1.0"?>'
+ '<soap:Envelope '
+ 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
+ '<soap:Header/>'
+ '<soap:Body xmlns:m="http://tempuri.org/">'
+ '<m:GetDoc>'
+ '<m:id>322332846</m:id>'
+ '<m:template>LuxuryCars</m:template>'
+ '</m:GetDoc>'
+ '</soap:Body>'
+ '</soap:Envelope>';
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: HTTP header

Post by Scott Klement »

Hello Giel,
  1. The http_setOption() should be used to set the SoapAction. In your code, you are just putting SoapAction into a variable, but then not using it.
  2. The content-type need not be set with http_setOption. Instead, it should be provided as a parameter to http_req() or similar.
  3. To add your own additional headers (such as the Identity: root one) you will need to code a special "exit procedure" (i.e. a callback). This will run in the background when HTTPAPI is sending the data over the network, and can be used to add additional data into the headers. It is up to you to format the header properly.
Here is an example:

Code: Select all

**free

ctl-opt dftactgrp(*no) bnddir('HTTPAPI');

 /copy httpapi_h

dcl-s rc int(10);
dcl-s response varchar(50000:4);
dcl-s SOAP     varchar(10000:4);


// This will create a log in the IFS containing information
// about what's happening under the covers.  helpful to 
// troubleshoot problems. (Not for use in production.)

http_debug(*on: '/tmp/httpapi_debug_trace.txt');


// Set up a subprocedure to be called to add additional
// headers to the request.

http_xproc( HTTP_POINT_ADDL_HEADER: %paddr(AddHeaders));


// Set up the SoapAction header

http_setOption('SoapAction': '"http://tempuri.org/IDMS/GetDoc"'); 


// Build the SOAP message 

SOAP = '<?xml version="1.0"?>'
     + '<soap:Envelope '
     +    'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
     +   '<soap:Header/>'
     +   '<soap:Body xmlns:m="http://tempuri.org/">'
     +     '<m:GetDoc>'
     +       '<m:id>322332846</m:id>'
     +       '<m:template>LuxuryCars</m:template>'
     +     '</m:GetDoc>'
     +   '</soap:Body>'
     + '</soap:Envelope>';


// Run the HTTP POST request.
//  The parameters are:
//     1) HTTP method (POST)
//     2) The URL
//     3) file to receive response to (*OMIT=none)
//     4) string to receive response to (response)
//     5) file to send data from (*OMIT=none)
//     6) string to send data from (SOAP)
//     7) Content-type of data being sent

rc = http_req( 'POST'
             : 'http://dms.qa.abgza.co.za/API/DMS.svc?singleWsdl'
             : *omit
             : response
             : *omit
             : SOAP
             : 'application/soap+xml; charset=utf-8');
if rc <> 1;
  // TODO: insert your own HTTP error handling code 
  //       instead of http_crash
  http_crash();
endif;        


// Disable the identity header.
// if not disabled, it will stay until the activation
// group ends.

http_xproc(HTTP_POINT_ADDL_HEADER: *NULL);
*inlr = *on;


/// ------------------------------------------------------------
//  AddHeaders
//  This is called by HTTPAPI in the background *during* the 
//  HTTP request to the network. It allows you to add your own 
//  headers to the HTTP document.
//
//  @param (output) a string containing any/all HTTP headers
//           to add. Headers must be in the format of
//           HeaderName: HeaderValue<CRLF>
/// ------------------------------------------------------------
dcl-proc AddHeaders;
  dcl-pi *n;
    toBeAdded varchar(32767);
  end-pi;

  toBeAdded = 'Identity: root' + x'0d25';

  // NOTE: If you wanted to add multiple headers, they would
  //       all be in the same string such as:
  //
  // toBeAdded = 'x-example-header: example value' + x'0d25'
  //           + 'HeaderTwo: data data data' + x'0d25'
  //           + 'Identity: root' + x'0d25';

end-proc;
I'm surprised to see someone still using SOAP in 2023. Almost everyone has moved to REST APIs at this point -- really unusual to still see a SOAP API. But the process is the same for REST or SOAP, just the format of the data is different.
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: HTTP header

Post by Scott Klement »

It's also worth noting that I've never heard of the "Identity" header before. It is a non-standard header, as far as I can tell.

Since it is non-standard, if you have any influence over whomever runs this API, you should suggest changing it to "x-identity" instead of just "identity". Non-standard headers should always begin with "x-". But, if whomever runs this API isn't willing to change it, using the above process will work fine. If they ever do add an 'identity' header as a standard, it will conflict with your extension to the standards, that's the only reason it should begin with an 'x-', to eliminate the possibility of the conflict.
Giel
Posts: 3
Joined: Tue Nov 21, 2023 4:40 am

Re: HTTP header

Post by Giel »

thank you for all the info, it is very much appreciated

I was given a choice between REST and SOAP, I just took the SOAP option because I was given a C# program to get the layout of the API.
Unfortunately I am not in a position to ask for changes so I will just have to make do with what I have at this point.

I was having issues and then discovered that the library LIBHTTP I was using was an older version where the setOption was not available yet, so some of my coding is still because of the older version of the library where I was using the http_url_post_raw command, busy redoing my coding now

if http_url_post_raw(url
: %addr(SOAP) + 2
: %len(SOAP)
: 1
: %paddr('INCOMING')
: HTTP_TIMEOUT
: HTTP_USERAGENT
: 'text/xml'
: %Trim(SoapAction)) =1;
Scott Klement
Site Admin
Posts: 658
Joined: Sun Jul 04, 2021 5:12 am

Re: HTTP header

Post by Scott Klement »

If it doesn't contain HTTP_setOption, then it is (at a minimum) 6 years out of date. Possibly older.

Please update to the current version of HTTPAPI. The update is quick and easy, contains all of the security and bug fixes made over the past 6+ years, and is backward compatible. (None of the existing programs need to be changed or recompiled.)

I cannot provide any support for old versions.
Giel
Posts: 3
Joined: Tue Nov 21, 2023 4:40 am

Re: HTTP header

Post by Giel »

thank you I have already upgraded the file I just need to now upgrade my program :lol:
Post Reply