Wrong response with HTTPAPI

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
g.scantee
Posts: 4
Joined: Fri Jun 10, 2022 8:22 am

Wrong response with HTTPAPI

Post by g.scantee »

Hi Everyone!

I'm looking for some help. I need to make an API call with a "GET" request.

RESPONSE = http_string('GET': url: request: 'application/json');

This API return me a PDF file in base64 format.
Everything works fine, but when I get the answer of the call, the response reverses the "//" to "\/"

For example:
The value it should have is this : RESPONSE = "TLs1x2//fvuUn2uWn7XH8vPr52CO579fP3//7//PL5x+f6+dfn//vndZv39c"
But it return me this: RESPONSE = "TLs1x2\/fvuUn2uWn7XH8vPr52CO579fP3\/7\/PL5x+f6+dfn\/vndZv39c"
As you can see the first "/" is reversed to "\"
How can i fix it?
Thanks in advance.
Scott Klement
Site Admin
Posts: 635
Joined: Sun Jul 04, 2021 5:12 am

Re: Wrong response with HTTPAPI

Post by Scott Klement »

I've never heard of HTTPAPI doing something like this before, and I can't think of any way it'd do that.

Can you tell me how to reproduce the problem?
g.scantee
Posts: 4
Joined: Fri Jun 10, 2022 8:22 am

Re: Wrong response with HTTPAPI

Post by g.scantee »

Here is all the code.

Code: Select all

**free
       ctl-opt option(*srcstmt:*NODEBUGIO) dftactGrp(*no)
               bnddir('HTTPAPI':'YAJL') DecEdit('0.');

       /copy httpapi_h
       /copy yajl_h

       dcl-pr cmd extpgm('QCMDEXC');
         command varchar(200) const;
         length packed(15:5) const;
       END-PR;


       // Global variable
       //dcl-s respx    varchar(80);
       dcl-s urlx     varchar(80);
       dcl-s ID_x     PACKED(3:0);
       DCL-S pippo      varchar(120);
       DCL-S DataInizPgm  Packed(8:0);
       DCL-S OraInizPgm   Packed(6:0);

       Exsr SetUp;
       if not translate();
         Exsr $Exit;
       ENDIF;
       Exsr $Exit;


       //--------------------------------------------------------
       // SetUp  subroutine
       //--------------------------------------------------------

       Begsr SetUp;

         DataInizPgm = %Dec(%Date():*Iso);                      //Data inizio pgm
         OraInizPgm  = %Dec(%Time():*Iso);                      //Ora inizio pgm
       ENDSR;
       //--------------------------------------------------------
       // Uscita dal programma
       //--------------------------------------------------------

       BEGSR $Exit;
         *Inlr = *On;
         Return;
       EndSr;

       //--------------------------------------------------------
       // Crea Json e invoca Web Service
       //--------------------------------------------------------
       dcl-proc translate;


         DCL-PI *N IND;

         END-PI;

         dcl-s url      varchar(2000);
         dcl-s request  varchar(2000);
         dcl-s response varchar(1000000); //1mb
         dcl-s output   varchar(1000);
         dcl-s errMsg   varchar(500);
         dcl-s wait     char(1);
         dcl-s docNode  like(yajl_val);
         dcl-s node     like(yajl_val);
         dcl-s httpcode varchar(1024);

         http_xproc(HTTP_POINT_ADDL_HEADER: %paddr(add_accept_header));


         yajl_genOpen(*OFF);

         yajl_beginObj();                     // {

         yajl_addChar('id':'9999999');

         yajl_endObj();                       // }

         request = yajl_copyBufStr();
         yajl_genClose();

         http_setOption('local-ccsid': '0');
         http_setOption('network-ccsid': '1208');

         url = 'https://Api-Key-example/api/';

         monitor;
           response = http_string('GET': url: request: 'application/json');
         on-error;
           httpcode = http_error();
         endmon;

         docNode = yajl_string_load_tree(response: errMsg);
         urlx  = url;

         RETURN *ON;
       end-Proc;

       // --------------------------------------------------
       // Set headers
       // --------------------------------------------------
       dcl-proc add_accept_header;
         dcl-pi *n;
           extraHeader varchar(1024);
         end-pi;

         dcl-c CRLF x'0d25';

         extraHeader += 'API-Key: ' + %Trim(Api) + CRLF;
         
       end-proc ;

       // -------------------------------------------------- 
       
Last edited by g.scantee on Sat Feb 25, 2023 9:48 am, edited 2 times in total.
jonboy49
Posts: 200
Joined: Wed Jul 28, 2021 8:18 pm

Re: Wrong response with HTTPAPI

Post by jonboy49 »

I don't have any of your valid item codes (or whatever they are) to test with and had to guess that you just added the value to the URL but ...

Using Insomnia to test the service I found that it can actually return \/ - so I'm guessing this is nothing to do with HTTPAPI and you may need to contact the vendor. Using the URL XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX the API returns:

Code: Select all

{
	"status": "KO",
	"err": "API sconosciuta: getLdv\/123"
}
Since this is JSON and it uses the \ as an escape character - my guess is that this is normal behaviour.

Two suggestions:
1) Use a tool like Insomnia or SoapUI or ... to test the call. I think you'll find it sees the same data as HTTPAPI

2) It is not a good idea to publish your personal access code in a public forum. Assuming that that is what you have done. Certainly, when I used that code I no longer got authorization errors.
Scott Klement
Site Admin
Posts: 635
Joined: Sun Jul 04, 2021 5:12 am

Re: Wrong response with HTTPAPI

Post by Scott Klement »

Yes, if indeed what you are seeing is JSON, the \ before the / is completely normal and expected. And it's not in any way related to HTTPAPI -- that's what the server is responding with, and it's doing that because it has to in order to follow JSON standards.

When you interpret that JSON with a tool that understands JSON (DATA-INTO, YAJL procedures, SQL, etc) those will be removed to make the code normal.

However, there was absolutely nothing in your original response that indicated JSON? What you had was this:

Code: Select all

RESPONSE = "TLs1x2\/fvuUn2uWn7XH8vPr52CO579fP3\/7\/PL5x+f6+dfn\/vndZv39c"
 
So please clarify... is the result a JSON document, or is it a string like the above?
g.scantee
Posts: 4
Joined: Fri Jun 10, 2022 8:22 am

Re: Wrong response with HTTPAPI

Post by g.scantee »

Hi Scott and everyone ,

I noticed that the result is not a json document. Returns a string format.
To be clear, on POSTMAN when I make the request it automatically converts the result to a JSON, for "pretty view", but if I change the return type from "JSON" to "TEXT", the result is identical to that of the HTTPAPI, i.e a string.

Just to clarify, the risult is in JSON String:

Code: Select all

{"ord_id":"xxxxxxx","ldv":"TLs1x2\/fvuUn2uWn7XH8vPr52CO579fP3\/7\/PL5x+f6+dfn\/vndZv39c
5Wf33\/++Kfy8+fPb\/vP\/\/gF\/rvy\/9O3ISvmyeTf3+zz559\/VdnE8h
7H5XSOKPsL5pY4e3xv53C0eXD33BJnj\/1xDkebB3fPLXH22KpzONo8uHtui
bPH2pzD0ebB3XNLnD0+3TkcbR7cPbfE2eP9OoejzYN7Defu0bhlOoejzYO75
5Y4Z0Q7NE5xp8s7Xd3homKYIW13BboL0HzfGHrsvvnmm++9+pYxtNh49X1X3"}
The response that i need, is this, in JSON type.

Code: Select all

{
"ord_id":"xxxxxxx",
"ldv":"TLs1x2/fvuUn2uWn7XH8vPr52CO579fP3/7/PL5x+f6+dfn/vndZv39c
5Wf33/++Kfy8+fPb/vP//gF/rvy/9O3ISvmyeTf3+zz559/VdnE8h
7H5XSOKPsL5pY4e3xv53C0eXD33BJnj/1xDkebB3fPLXH22KpzONo8uHtui
bPH2pzD0ebB3XNLnD0+3TkcbR7cPbfE2eP9OoejzYN7Defu0bhlOoejzYO75
5Y4Z0Q7NE5xp8s7Xd3homKYIW13BboL0HzfGHrsvvnmm++9+pYxtNh49X1X3"
}
without "\"

Is possible make it to return me a response in JSON format and not in JSON String?

If not, how can i convert the response that is in string format to the JSON format? Can you help us.

Thanks in advance.
Scott Klement
Site Admin
Posts: 635
Joined: Sun Jul 04, 2021 5:12 am

Re: Wrong response with HTTPAPI

Post by Scott Klement »

I don't understand what you are saying.

First you say it's not a JSON document -- then you show me a JSON document.... Then you ask me how you can make it not be a JSON document.

to be absolutely clear: HTTPAPI merely downloads, byte-for-byte what the HTTP server sends. It does not change the format of the data in any way, except in some circumstances it will convert it to EBCDIC.

It never inserts \ characters or converts it from a string to a JSON document. That is simply what the server is sending you.

Just use a JSON tool to interpret it -- this isn't rocket science.
Post Reply