HOW TO GET data JSON from HTTP POST with RPG.

Use this board to ask questions or have discussions with other Universal Displays users.
iclassic
New User
Posts: 16
Joined: Thu Dec 07, 2017 10:13 pm
First Name: chatchawan
Last Name: phuttha
Company Name: AYCAL
City: bangkok
Country: Thailand
Contact:

HOW TO GET data JSON from HTTP POST with RPG.

Post 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).
Attachments
How To Get JSON DATA_Client request  HTTP method POST and Send data to insert table on IBM i.jpg
How To Get JSON DATA_Client request HTTP method POST and Send data to insert table on IBM i.jpg (187.03 KiB) Viewed 25404 times
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

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

Post 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
iclassic
New User
Posts: 16
Joined: Thu Dec 07, 2017 10:13 pm
First Name: chatchawan
Last Name: phuttha
Company Name: AYCAL
City: bangkok
Country: Thailand
Contact:

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

Post 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')));
Attachments
2.jpg
2.jpg (141.02 KiB) Viewed 25398 times
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

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

Post 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;
iclassic
New User
Posts: 16
Joined: Thu Dec 07, 2017 10:13 pm
First Name: chatchawan
Last Name: phuttha
Company Name: AYCAL
City: bangkok
Country: Thailand
Contact:

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

Post by iclassic »

To Scott Klement.
Please help me again. Compile Error.
Attachments
3.jpg
3.jpg (206.54 KiB) Viewed 25391 times
Scott Klement
Experienced User
Posts: 2711
Joined: Wed Aug 01, 2012 8:58 am
First Name: Scott
Last Name: Klement
Company Name: Profound Logic
City: Milwaukee
State / Province: Wisconsin

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

Post by Scott Klement »

Hmm... works for me. Maybe you're not up-to-date on PTFs?
iclassic
New User
Posts: 16
Joined: Thu Dec 07, 2017 10:13 pm
First Name: chatchawan
Last Name: phuttha
Company Name: AYCAL
City: bangkok
Country: Thailand
Contact:

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

Post by iclassic »

Thank you...
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest