IBM i Global

Β View Only

 QSYS2.HTTP_POST Header Error

Doug Freeman's profile image
Doug Freeman posted Fri March 06, 2026 04:11 PM

Hello. I'm trying to use an SQLRPGLE with QSYS2.HTTP_POST to access a UPS site and retrieve a token. I've been getting an error message 'INVALID PROPERTY: Headers'. I've tried different syntaxes for the header lines in the SQLRPGLE but I still get this error. I can remove the header lines (leave the 3rd parm blank) from QSYS2.HTTP_POST and connect to the UPS site. UPS then sends me a response file telling me 'Invalid/Missing Authorization Header' which is to be expected but proves I can reach the UPS site and tells me my header lines are not correct.  

Does anyone have any thoughts on why I'm getting this error and what can be done to resolve it? I've been programming in RPG since 1983 but I'm new to using these QSYS2.HTTP commands. Any help would be appreciated.

ERROR

 Message ID . . . . . . :   CPF503E       Severity . . . . . . . :   30
 Date sent  . . . . . . :   03/06/26      Time sent  . . . . . . :   08:46:52
 Message type . . . . . :   Sender copy

 Message . . . . :   User-defined function error on member QSQPTABL.
 Cause . . . . . :   An error occurred while invoking user-defined function
   HTTP_POST in library QSYS2. The error occurred while invoking the associated
   external program or service program QSQAXISC in library QSYS, program entry
   point or external name axiscPostClob, specific name HTTP_POST. The error
   occurred on member QSQPTABL file QSQPTABL in library QSYS2. The error code
   is 1. The error codes and their meanings follow:
     1 -- The external program or service program returned SQLSTATE 38501. The
   text message returned from the program is: ERROR  : INVALID PROPERTY :
   Headers .
     2 -- The external program failed before it completed.
     3 -- The database timed out waiting for the program to return. The timeout
   value used by the database was 0 minutes and 30 seconds.
     4 -- The external program no longer exists or is not found.
     5 -- One of the input parameters of the function had a data mapping error.
     6 through 26 -- See the previous message in the joblog.
     For an external program, the program entry point displayed will be *N.
 Recovery  . . . :   For error codes 1 and 2, determine the cause of the error
   from either the SQLSTATE or a previously listed message.

PROGRAM

  CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++++++Len++D+HiLoEq....C
0 C                   clear                   cli
0 C                   eval      cli = %trim(clientid) + ':' + %trim(clientsc)
0 C                   clear                   base64
0 C*
0  /free
0     exec sql
0     set :base64=QSYS2.BASE64_ENCODE(:cli);
0   replystmf_Name = '/dfreeman/upu178h305rm.txt';
0   replystmf_NL   = %len(%trimr(replystmf_name));
0   replystmf_FO   = SQFOVR;
0    Exec Sql
0     VALUES QSYS2.HTTP_POST(
0               'https://wwwcie.ups.com/security/v1/oauth/token',
0               '',
0               '{"Headers":{"Authorization":"'|| :base64 ||'",+
0                            "x-merchant-id":"AAAAAA",+
0                 "Content-Type":"application/x-www-form-urlencoded"}}')
0                              into :replystmf;
0  /end-free
0 C*

Jack Woehr's profile image
Jack Woehr IBM Champion

2 problems:

1. You passed an empty string for argument 2 (Headers) and put Headers in the JSON body.
2. The string after Authorization should start with Basicfollowed by a space.

Jack Woehr's profile image
Jack Woehr IBM Champion

Here's how Google Gemini rewrites it! Haven't tried this ... AI can be silly ... but .. FWIW YMMV 😁

/free
    // 1. Generate the Base64 string
    // The SQL engine handles the conversion from EBCDIC to UTF-8 for the encode
    exec sql 
      set :base64 = QSYS2.BASE64_ENCODE(trim(:clientid) || ':' || trim(:clientsc));

    // 2. Setup the stream file for the response
    replystmf_Name = '/dfreeman/upu178h305rm.txt';
    replystmf_NL   = %len(%trimr(replystmf_name));
    replystmf_FO   = SQFOVR;

    // 3. Execute the POST correctly
    Exec Sql
     VALUES QSYS2.HTTP_POST(
       -- Parameter 1: The URL
       'https://wwwcie.ups.com/security/v1/oauth/token',

       -- Parameter 2: THE HEADERS (Must include "Basic " prefix)
       -- We CAST to CCSID 1208 (UTF-8) to ensure the API receives valid text
       CAST('{"Headers":{' || 
            '"Authorization":"Basic ' || :base64 || '",' ||
            '"x-merchant-id":"AAAAAA",' ||
            '"Content-Type":"application/x-www-form-urlencoded"' ||
            '}}' 
       AS VARCHAR(1000) CCSID 1208),

       -- Parameter 3: THE BODY (Required for OAuth token requests)
       CAST('grant_type=client_credentials' AS VARCHAR(100) CCSID 1208)
     )
     INTO :replystmf;
 /end-free