Resolving SQLSTATE Errors When Making HTTP GET Requests with SYSTOOLS.HTTPGETCLOBVERBOSE in RPGLE

SQLSTATE=1H52/ SQLCODE=+462 with UPS API Tracking HTTPGETCLOBVERBOSE Request

Introduction to RPGLE and REST APIs

As a developer, it’s essential to understand how to interact with external APIs in RPGLE programming. One such API is the UPS API for tracking packages. In this article, we’ll delve into the challenges of making HTTP GET requests using the SYSTOOLS.HTTPGETCLOBVERBOSE function in RPGLE and explore possible solutions.

Understanding SQLSTATE and SQLCODE

When working with RPGLE, it’s crucial to understand the different error codes that can occur during program execution. The SQLSTATE and SQLCODE variables are used to indicate the type of error encountered.

  • SQLSTATE: This variable contains a six-character code that represents the severity level of the error.
  • SQLCODE: This variable contains an eight-digit code that corresponds to the specific error message.

In this case, we’re encountering errors with SQLSTATE=1H52 and SQLCODE=+462. These values indicate that there’s a problem parsing the SQL statement or passing invalid parameters.

The Challenge of Converting SYSTOOLS.HTTPGETCLOBVERBOSE

The SYSTOOLS.HTTPGETCLOBVERBOSE function is used to make HTTP GET requests. However, when trying to convert this function from an ACS (Advanced Client Server) script to a RPGLE program, we encounter issues with parsing the URL and headers.

Parsing the URL and Headers

In the provided ACS script, the httpgetclobverbose function takes two parameters: the URL and the header string. The header string is formatted as follows:

<httpHeader>
<header name="Authorization" value="Basic xxxacessTokenxxx(1308 charaters)"/>
<header name="transId" value="string"/>
<header name="transactionSrc" value="testing"/>
</httpHeader>

In our RPGLE program, we need to replicate this format using the EVAL statement and the %SUBST function.

Replicating the Header Format in RPGLE

To create the header string in RPGLE, we can use the following code:

EVAL      myheader='<httpHeader><header name="Authorization" value="Basic '
     +%TRIM(accessToken) + '"/><header name="transId" value="string"/><header 
     +name="transactionSrc" value="testing"/></htt'
     +pHeader>'

This code uses the EVAL statement to create a string that matches the format of the ACS script. The %TRIM function is used to trim any whitespace from the access token.

Substringing the URL and Headers

To substring the URL and headers into separate variables, we can use the following code:

EVAL      myURL = 'https://wwwcie.ups.com/api/track/v1'
             +'/details/xxtrackingnumberxx?locale=en_US&r'
             +'eturnSignature=false&returnMilestones=false&returnPOD=false'

EVAL      myheader='<httpHeader><header name="Authorization" value="Basic '+
     +%TRIM(accessToken) + '"/><header name="transId" value="string"/><header 
     +name="transactionSrc" value="testing"/></htt'
     +pHeader>'

Resolving the Issue

After creating the header string and substringing the URL and headers, we can use the SYSTOOLS.HTTPGETCLOBVERBOSE function to make the HTTP GET request.

/EXEC SQL
C+ SELECT  CAST(RESPONSEMSG AS CLOB(1000000)),
C+         CAST(responseHttpHeader  AS CLOB(1000000))
C+         INTO :myClob:null,  :respHeader
C+         FROM TABLE( SYSTOOLS.HTTPGETCLOBVerbose(
C+                    CAST(:myURL AS VARCHAR(512)),
C+                    CAST (:myheader AS CLOB(2K))))
C/END-EXEC

However, we’re still encountering issues with the SQLSTATE and SQLCODE variables. This is likely due to the fact that we’re not properly parsing the response message.

Resolving the SQLSTATE and SQLCODE Issues

To resolve the SQLSTATE and SQLCODE issues, we need to ensure that we’re correctly parsing the response message. We can do this by using the %SUBST function to extract the relevant data from the response message.

EVAL      myClob_data = %SUBST(myClob_data:1:myClob_len)
EVAL      respHeader_DATA = %SUBST(respHeader_DATA:1:respHeader_LEN)

Conclusion

In this article, we explored the challenges of making HTTP GET requests using the SYSTOOLS.HTTPGETCLOBVERBOSE function in RPGLE. We discussed the issues with parsing the URL and headers, as well as resolving the SQLSTATE and SQLCODE errors.

By following the steps outlined in this article, you should be able to successfully make HTTP GET requests using the SYSTOOLS.HTTPGETCLOBVERBOSE function in your RPGLE program.


Last modified on 2024-12-28