Page 1 of 1

DATA-INTO Error

Posted: Mon Feb 03, 2025 8:22 pm
by markl
This is a follow up post to the issue I was having with HTTPAPI. That issue has been resolved.

I have a simple authorization routine. I copied much of the code from the recent OATH2 post. I POST a clientId and clientSecret to a webservice and it returns an expires variable and an access token variable in JSON. I'm trying to use the DATA-INTO procedure to parse the JSON.

I'm able to retrieve the resultstr vaiable and it appears to be formatted correctly. When the code hits the DATA-INTO code, I get the error: "The document for the DATA-INTO operation does not match the RPG variable". I've reviewed the related posts but haven't found an answer. Can anyone help?

I believe I have the qualifying variables set up correctly to match the return string.

resultStr displays as: {"Expires":180,"AccessToken":"xxxx... ...xxx"}

Code: Select all

H DFTACTGRP(*NO)                                     
H BNDDIR('HTTPAPI':'YAJL')                           
 /include yajl_h                                     
 /copy httpapi_h                                     
                                                     
  dcl-s clientid       varchar(4096) inz(            
    'xxxxxxxxxxxxxxxxxxxxxxx');             
  dcl-s clientsecret   varchar(4096) inz(            
    '$$$$$$$$$$$$$$$$$$$$$$$$$$$$$');  
                                                     
  dcl-s sendData       varchar(16384);               
                                                     
  dcl-ds authResult qualified;                       
    Expires          int(3);                              
    AccessToken varchar(4096);                       
  end-ds;                                            
                                                     
  dcl-s resultStr      varchar(16000000);            
  d msg             s             52A                                                  
  d rc              s             10I 0                                                
                                                                                     
     http_debug(*on);                                                                
     *inlr = *on;                                                                    
                                                                                     
     //                                                                              
     //  Build the variables                                                         
     //                                                                              
                                                                                     
     sendData = 'clientId=' + http_urlEncode(clientid)                               
              + '&clientSecret=' + http_urlEncode(clientsecret);                     
                                                                                     
     //                                                                              
     //  post the credentials to retrieve the token                                  
     //                                                                              
                                                                                     
     rc = http_req( 'POST'                                                           
      :'https://xxxx.com/api/authenticate'              
              : *omit  
              : resultStr // String to receive the results              
              : *omit                                                   
              : sendData                                                
              : 'application/x-www-form-urlencoded');                   
                                                                        
    //                                                                     
    // error checking                                                      
    //                                                                     
                                                                        
    if (rc <> 1);                                                          
      msg = http_error();                                                  
      dsply msg;                                                           
      return;                                                              
    else;                                                                  
      msg = resultStr;                                                     
      dsply msg;                                                           
      data-into authResult %DATA(resultStr)                                
                        %PARSER('YAJLINTO');                            
    endif;    
    return;   
 

Re: DATA-INTO Error

Posted: Mon Feb 03, 2025 11:12 pm
by jonboy49
You need to add 'case=any' as an option. Like so:

Code: Select all

data-into authResult %DATA(resultStr : 'Case=any')                                
                        %PARSER('YAJL/YAJLINTO');           
You have to tell RPG to convert the case of the elements in the JSON before it attempts to match them to the RPG fields. RPG upper-cases all variable names so right now it is attempting to match the RPG field 'EXPIRES' with the JSON 'Expires' and they don't match.

P.S. When handling some JSON docs you will need to use case=convert to deal with non-RPG characters in JSON names.

Re: DATA-INTO Error

Posted: Tue Feb 04, 2025 6:45 pm
by markl
Issue resolved. Thank you!

- markl