Introduction

In this module, we will explore how to integrate RPG with modern technologies, focusing on APIs and JSON. Understanding these concepts is crucial for developing applications that can communicate with other systems and services over the web.

What is an API?

An API (Application Programming Interface) is a set of rules and protocols for building and interacting with software applications. APIs allow different software systems to communicate with each other.

Key Concepts:

  • Endpoints: Specific URLs where API services are accessed.
  • HTTP Methods: Common methods include GET, POST, PUT, DELETE.
  • Request and Response: Data sent to and received from the API.

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate.

JSON Structure:

  • Objects: Enclosed in curly braces {} and contain key-value pairs.
  • Arrays: Enclosed in square brackets [] and contain a list of values.
  • Values: Can be strings, numbers, objects, arrays, true, false, or null.

Example JSON:

{
  "name": "John Doe",
  "age": 30,
  "isEmployee": true,
  "skills": ["RPG", "SQL", "Java"],
  "address": {
    "street": "123 Main St",
    "city": "Anytown",
    "zipcode": "12345"
  }
}

Using APIs in RPG

To interact with APIs in RPG, you typically use HTTP functions to send requests and handle responses. Let's go through a practical example.

Example: Fetching Data from an API

We'll use a simple public API to fetch user data.

Step-by-Step Guide:

  1. Define the API Endpoint:

    Dcl-S apiUrl Varchar(256) Inz('https://jsonplaceholder.typicode.com/users/1');
    
  2. Send HTTP GET Request:

    Dcl-S httpResponse Varchar(32767);
    Dcl-S httpStatus Int(10);
    
    // Use HTTP API to send GET request
    httpStatus = http_url_get(apiUrl: httpResponse);
    
  3. Check Response Status:

    If httpStatus = 200;
       // Successful response
       Dsply httpResponse;
    Else;
       // Handle error
       Dsply 'Error fetching data';
    EndIf;
    

Parsing JSON in RPG

To parse JSON data in RPG, you can use the JSON_TABLE function or a JSON parsing library.

Example: Parsing JSON Response

Assume the response is:

{
  "id": 1,
  "name": "Leanne Graham",
  "username": "Bret",
  "email": "[email protected]"
}

Step-by-Step Guide:

  1. Define Data Structure for JSON:

    Dcl-Ds userData Qualified;
       id Int(10);
       name Varchar(50);
       username Varchar(50);
       email Varchar(50);
    End-Ds;
    
  2. Parse JSON Response:

    // Assuming a JSON parsing procedure 'parseJson' is available
    parseJson(httpResponse: userData);
    
  3. Access Parsed Data:

    Dsply userData.name; // Displays "Leanne Graham"
    

Practical Exercise

Task:

Write an RPG program that fetches a list of users from the API https://jsonplaceholder.typicode.com/users and displays the name and email of each user.

Solution:

Dcl-S apiUrl Varchar(256) Inz('https://jsonplaceholder.typicode.com/users');
Dcl-S httpResponse Varchar(32767);
Dcl-S httpStatus Int(10);

// Define data structure for user
Dcl-Ds user Qualified;
   id Int(10);
   name Varchar(50);
   username Varchar(50);
   email Varchar(50);
End-Ds;

// Define array to hold users
Dcl-S users LikeDs(user) Dim(10);

// Send HTTP GET request
httpStatus = http_url_get(apiUrl: httpResponse);

If httpStatus = 200;
   // Parse JSON response into users array
   parseJson(httpResponse: users);

   // Display user names and emails
   For i = 1 to %Elem(users);
      Dsply users(i).name + ' - ' + users(i).email;
   EndFor;
Else;
   Dsply 'Error fetching data';
EndIf;

Common Mistakes and Tips

  • Incorrect URL: Ensure the API endpoint URL is correct.
  • HTTP Methods: Use the correct HTTP method (GET, POST, etc.) as required by the API.
  • Error Handling: Always check the response status and handle errors appropriately.
  • JSON Parsing: Ensure the JSON structure matches the data structure in your RPG program.

Conclusion

In this section, we learned about APIs and JSON, how to send HTTP requests from RPG, and how to parse JSON responses. These skills are essential for integrating RPG applications with modern web services and external systems. In the next module, we will explore XML handling, another important data interchange format.

© Copyright 2024. All rights reserved