http_req

Discussions related to HTTPAPI (An HTTP Client Package for RPG programming.) http://www.scottklement.com/httpapi/
Post Reply
almond
Posts: 5
Joined: Fri Jul 26, 2024 6:11 am

http_req

Post by almond »

Dear
When using http_req, the "File to receive" function works fine, but how can I use the "String to receive" option to receive a JSON body like this?
{
"encryptString": "138",
"hexString": "5BF28981860"
}

my code

Code: Select all

D rc              s             10I 0                        
D msg             s             25A                          
D url1            s            300A                          
D url2            s            300A                          
D parm1           s            100A                          
D parm2           s              8A                          
D responseBody    s               a   len(16000000) varying  
D debugLog        s             32A   varying inz            
D urlFull         s          32767a   varying                
D res             s             50a                          
 /free                                                       
                                                             
    url1 = 'http://192.168.0.1:9090/test/api';                                  
    parm1 = 'encryptString=138';                                                                           
    urlFull = %trim(url1) + %trim(parm1);      
                                                             
    debugLog = '/home/qsnp252/' ;                            
    http_debug(*ON: debugLog + 'testrest.log');    
    http_setOption( 'content-type'                                    
              : 'application/json' );                             
                                                                  
rc = http_req( 'GET'                                              
              : urlFull                                           
              : '/home/qsnp252/test.json'    // File to receive   
              : responseBody                 // String to receive 
              : *omit                        // File to send      
              : *omit                        // String to send    
             );                                                   
if rc <> 1;                                                       
     msg = http_error();                                          
     dsply ('error:' + msg);                                      
else;                                                             
   res = %subst(responseBody : 1 : 50);                           
   dsply res;                                                     
   dsply 'OK';                                                    
endif;                                                      
   https_cleanup(); 
   *inlr = *on;     
    return;         
/end-free                 
stefan@tageson.se
Posts: 23
Joined: Wed Jul 28, 2021 7:55 am
Location: Viken, Sweden
Contact:

Re: http_req

Post by stefan@tageson.se »

Unless I'm misunderstanding something - just code *OMIT for the file to receieve parameter.
You can't use both file to receive and string to receive in the same call.
almond
Posts: 5
Joined: Fri Jul 26, 2024 6:11 am

Re: http_req

Post by almond »

Thanks for your reply!
it can works using both parameters ,but i don't know hot to parse the responseBody like json body .
almond
Posts: 5
Joined: Fri Jul 26, 2024 6:11 am

Re: http_req

Post by almond »

i'm sorry, i set parameter of ResultStmf is *OMIT
i code like this could work,the response could display the json result
thank you very much!

Code: Select all

%size(%trim(responseBody ))
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: http_req

Post by Scott Klement »

almond wrote: Fri Jul 26, 2024 11:15 am i code like this could work,the response could display the json result
thank you very much!

Code: Select all

%size(%trim(responseBody ))
I don't understand this at all.

Note that %SIZE is very different from %LEN. %SIZE tells you how many bytes of memory the variable is using, whereas %LEN tells you the current length of the data in the string. So %SIZE(%TRIM()) doesn't make any sense... trimming blanks from the data has no impact on how much memory the variable occupies.

Furthermore, I don't understand where you would use this. You can't pass the size to http_req, so what are you using it for?

The length of the data (returned via %LEN) is set by http_req, so unless the server is adding extra leading or trailing blanks, there's no reason to use %TRIM. This whole discussion makes no sense.
almond
Posts: 5
Joined: Fri Jul 26, 2024 6:11 am

Re: http_req

Post by almond »

i just want to dsply the responsbody to verify the results

Code: Select all

dsply %size(%trim(responseBody));     // display the json body size
dsply %subst(%trim(res: 1 :50);  
Scott Klement
Site Admin
Posts: 872
Joined: Sun Jul 04, 2021 5:12 am

Re: http_req

Post by Scott Klement »

Then you should be doing %len(responseBody) not %size(%trim(responsebody))

almond wrote: Mon Jul 29, 2024 1:29 am i just want to dsply the responsbody to verify the results

Code: Select all

dsply %size(%trim(responseBody));     // display the json body size
dsply %subst(%trim(res: 1 :50);  
almond
Posts: 5
Joined: Fri Jul 26, 2024 6:11 am

Re: http_req

Post by almond »

thx very much
then I'll try the YAJL to analysis the respondy
Post Reply