Page 1 of 1

HOW TO GET data JSON from HTTP POST with RPG.

Posted: Thu Dec 07, 2017 10:35 pm
by iclassic
RPG With UNIVERSAL DISPLAY
1. Please See attach.
2. p_JSON = getenv('PUI_UNIVERSAL_INPUT');
3. Client request HTTP method POST and Send data to insert table on IBM i.
Send Data for insert (JSON format) in Body-POST.
4. RPG code (How to get or Extract Data (JSON Body-POST) for insert Table in IBM i).

Re: HOW TO GET data JSON from HTTP POST with RPG.

Posted: Fri Dec 08, 2017 12:05 am
by Scott Klement
You mean you want to know how to parse (or "process") the JSON data, correct? Your example already explains how to "get" it.

There are many options available for processing JSON in RPG.

1) IBM has added the JSON_TABLE keyword to SQL. This can parse JSON, and is included with a technology refresh for 7.2 and 7.3, so many people already have it. I am not an expert on it's use, and can't help you much, but you can read about it here:
https://www.ibm.com/developerworks/comm ... 20function

2) I use the open source tool called YAJL. This is a JSON parser written in C, and I've ported it to ILE C on IBM i. I've also written RPG "wrappers" to make it easy to use from RPG. This is the fastest tool that I've found... you can download a copy here:
http://www.scottklement.com/yajl/

There are newer examples of using YAJL in my "Calling Watson" and "Providing/Consuming Web Services" talks. There are copies of the slides and sample code from those talks found here:
http://www.scottklement.com/presentations/

3) Others such as Henrik Rutzou and Mihael Schmidt have released their own free tools for processing JSON in RPG. I do not have any experience with them, I only know that they exist. I did compare their speeds vs. YAJL several years ago, and at that time, YAJL was faster.

Hope that helps

Re: HOW TO GET data JSON from HTTP POST with RPG.

Posted: Fri Dec 08, 2017 1:35 am
by iclassic
1. http://www.profoundlogic.com/docs/displ ... Validation
2. The environment variable 'PUI_UNIVERSAL_INPUT' will be set to a copy of the HTTP input. Text data is encoded in UTF-8 and null-terminated.

3. I hope to see JSON In Field p_JSON.
{
"cusid": "test"
}

Ex.RPG Code...
p_JSON = getenv('PUI_UNIVERSAL_INPUT');
JSONLen = %int(%Str(getenv('CONTENT_LENGTH')));

Re: HOW TO GET data JSON from HTTP POST with RPG.

Posted: Fri Dec 08, 2017 4:28 am
by Scott Klement
As you've already mentioned, the encoding is UTF-8 and null-terminated.

Why do you say "the encoding is UTF-8" and then ask "what is the encoding"? This is strange.

RPG has a built-in function called %STR that is meant for retrieving null-terminated strings. You are using this for the other variables (request method, content type and content length). The only difference with the PUI_UNIVERSAL_INPUT is that the data is in UTF-8 instead of EBCDIC.

I do not recommend translating the data to EBCDIC because JSON is not designed for EBCDIC. JSON works best in UTF-8 format.

However, if you must have it in EBCDIC for some reason, you can translate it using the iconv() API, or if you are on V7R2 or newer (hopefully you are) then you can use RPG's built-in support for UTF-8 to translate it.

For example:

Code: Select all

     D p_JSON          s               *

     D                 ds
     D   JSON_RAW               1000000a   varying
     D   JSON_UTF8              1000000a   varying ccsid(*utf8)
     D                                     overlay(JSON_RAW)

     D JSON_EBCDIC     s        1000000A   varying     

      /free
      
        monitor;
           REQUEST_METHOD = %str(getenv('REQUEST_METHOD'));
           CONTENT_TYPE   = %str(getenv('CONTENT_TYPE'));
        on-error;
        endmon;

        p_JSON = getenv('PUI_UNIVERSAL_INPUT');

        if REQUEST_METHOD = 'POST'
           and CONTENT_TYPE = 'application/json';
           JSON_RAW = %str(p_JSON);
        endif;

        // When you copy data from a UTF-8 field to an 
        // EBCDIC field, RPG automatically translates it:
        
        JSON_EBCDIC = JSON_UTF8;

Re: HOW TO GET data JSON from HTTP POST with RPG.

Posted: Fri Dec 08, 2017 6:36 am
by iclassic
To Scott Klement.
Please help me again. Compile Error.

Re: HOW TO GET data JSON from HTTP POST with RPG.

Posted: Fri Dec 08, 2017 1:45 pm
by Scott Klement
Hmm... works for me. Maybe you're not up-to-date on PTFs?

Re: HOW TO GET data JSON from HTTP POST with RPG.

Posted: Sat Dec 09, 2017 11:20 am
by iclassic
Thank you...