http-getrpgle

Using QSYS2.HTTP_GET in RPGLE and having Bad request (400) error from the server


I am trying to call external API using QSYS2.HTTP_GET function using SQLRPGLE, having Bad request error from the server. I have used some variables to build the URL request and option parameter. Need to know why it giving Bad request response since I did not able to find request syntax error.

 H*
 D*-------------------------------------------------------------------------
 D*-------------------------------------------------------------------------
 DURL1             S            200A
 DURL2             S            200A
 Dxxxx             S             30A
 Dxzzz             S             30A
 Daaa              S             30A
 Dbbb              S             30A
 Dbbbsdn           S             30A
 Dbbbid           S             30A
 DOPTA1            S             30A
 C*-------------------------------------------------------------------------
 C
 C                   EVAL      OPTA1='{"sslTolerate":"true"}'
 C                   EVAL      xxxx='xxxx=xxxxxx'
 C                   EVAL      zzzz='&zzzz=c=xxxxx'
 C                   EVAL      ='&bbb=xxxxxx'
 C                   EVAL      aaa='&aaa=TESTSMS'
 C                   EVAL      bbb='&bbbsdn=xxxxxxxx'
 C                   EVAL      bbbsid='&bbbsid=xxxxxx'
 C                   EVAL      URL1='http://xyz.xxxx.com'+
 C                                  '/xxxxapi/xxxx/sss.com?'+
 C                                  %TRIM(%CHAR(xxxx))+
 C                                  %TRIM(%CHAR(zzzz))+
 C                                  %TRIM(%CHAR(aaa))+
 C                                  %TRIM(%CHAR(bbb))+
 C                                  %TRIM(%CHAR(bbbsdn))+
 C                                  %TRIM(%CHAR(bbbsid))
 C                   EVAL      URL2=%TRIM(%CHAR(URL1))
 C/EXEC SQL
 C+                  SET OPTION COMMIT=*NONE
 C/END-EXEC
 C/EXEC SQL
 C+                   INSERT INTO QGPL/SMSTST
 C+                   SELECT CAST(QSYS2.HTTP_GET(:URL2,:OPTA1)
 C+                   AS VARCHAR(2048) CCSID 37)
 C+                   FROM SYSIBM.SYSDUMMY1
 C/END-EXEC
 C                   SETON                                          LR 

I am expecting success 200 response from the server. Need to know why it giving Bad request response since I did not able to find request syntax error.


Solution

  • Instead of using fixed width character fields, you should using VARCHAR. Then you won't need the %TRIM(). You shouldn't need the %CHAR() either.

    I don't see anything wrong off the top of my head. I'd suggest running in debug and examining the values for URL2 and OPTA1 before the SQL statement runs.

    One possible issue might be the use of fixed width fields. The following does nothing as URL2 is still fixed width.

    URL2=%TRIM(%CHAR(URL1))
    

    I think the SQL engine would ignore the extra whitespace. But I can't say for sure. I always use VARCHAR.