Introduction

Web services allow different applications to communicate with each other over the internet. In RPG programming, integrating web services can enable your RPG applications to interact with other systems, fetch data from external sources, and provide services to other applications.

Key Concepts

  1. Web Services: A method of communication between two electronic devices over a network.
  2. SOAP (Simple Object Access Protocol): A protocol for exchanging structured information in the implementation of web services.
  3. REST (Representational State Transfer): An architectural style for designing networked applications.
  4. HTTP Methods: Common methods include GET, POST, PUT, DELETE.
  5. JSON (JavaScript Object Notation): A lightweight data-interchange format.
  6. XML (eXtensible Markup Language): A markup language that defines a set of rules for encoding documents.

Setting Up for Web Services in RPG

Before you can start working with web services in RPG, you need to ensure your development environment is set up to handle HTTP requests and parse JSON or XML responses.

Prerequisites

  • HTTP APIs: Ensure you have access to HTTP APIs for making requests.
  • JSON/XML Parsers: Libraries or built-in functions to parse JSON or XML data.

Making HTTP Requests

RPG can make HTTP requests using built-in functions or third-party libraries. Below is an example of making a simple GET request to a web service.

Example: Making a GET Request

**free
ctl-opt dftactgrp(*no) actgrp('QILE');

dcl-pr HttpGetRequest pointer extproc('QtmhGetEnv');
  requestUrl pointer value options(*string);
end-pr;

dcl-pr HttpGetResponse pointer extproc('QtmhGetEnv');
  responseBuffer pointer value options(*string);
end-pr;

dcl-s requestUrl varchar(256);
dcl-s responseBuffer varchar(1024);
dcl-s response pointer;

requestUrl = 'http://api.example.com/data';
response = HttpGetRequest(%addr(requestUrl));

if response <> *null;
  responseBuffer = %str(response);
  dsply responseBuffer;
else;
  dsply 'Error making HTTP request';
endif;

*inlr = *on;

Explanation

  • HttpGetRequest: A procedure to make an HTTP GET request.
  • requestUrl: The URL of the web service.
  • responseBuffer: A buffer to store the response from the web service.
  • response: A pointer to the response data.

Parsing JSON Responses

Once you have the response from the web service, you need to parse the JSON data to extract useful information.

Example: Parsing JSON Response

**free
ctl-opt dftactgrp(*no) actgrp('QILE');

dcl-pr HttpGetRequest pointer extproc('QtmhGetEnv');
  requestUrl pointer value options(*string);
end-pr;

dcl-pr JsonParse pointer extproc('QtmhGetEnv');
  jsonString pointer value options(*string);
end-pr;

dcl-s requestUrl varchar(256);
dcl-s responseBuffer varchar(1024);
dcl-s jsonString varchar(1024);
dcl-s jsonObject pointer;

requestUrl = 'http://api.example.com/data';
response = HttpGetRequest(%addr(requestUrl));

if response <> *null;
  responseBuffer = %str(response);
  jsonObject = JsonParse(%addr(responseBuffer));
  // Extract data from jsonObject
else;
  dsply 'Error making HTTP request';
endif;

*inlr = *on;

Explanation

  • JsonParse: A procedure to parse JSON data.
  • jsonString: The JSON string to be parsed.
  • jsonObject: A pointer to the parsed JSON object.

Practical Exercise

Task

Create an RPG program that makes a GET request to a public API (e.g., a weather API) and displays the current temperature.

Solution

**free
ctl-opt dftactgrp(*no) actgrp('QILE');

dcl-pr HttpGetRequest pointer extproc('QtmhGetEnv');
  requestUrl pointer value options(*string);
end-pr;

dcl-pr JsonParse pointer extproc('QtmhGetEnv');
  jsonString pointer value options(*string);
end-pr;

dcl-s requestUrl varchar(256);
dcl-s responseBuffer varchar(1024);
dcl-s jsonString varchar(1024);
dcl-s jsonObject pointer;
dcl-s temperature varchar(10);

requestUrl = 'http://api.weatherapi.com/v1/current.json?key=YOUR_API_KEY&q=London';
response = HttpGetRequest(%addr(requestUrl));

if response <> *null;
  responseBuffer = %str(response);
  jsonObject = JsonParse(%addr(responseBuffer));
  // Assuming the JSON structure has a field "temp_c" for temperature in Celsius
  temperature = GetJsonValue(jsonObject, 'temp_c');
  dsply 'Current Temperature: ' + temperature;
else;
  dsply 'Error making HTTP request';
endif;

*inlr = *on;

Explanation

  • GetJsonValue: A hypothetical function to extract a value from the JSON object.
  • temperature: The extracted temperature value from the JSON response.

Summary

In this section, you learned how to:

  • Understand the basics of web services.
  • Make HTTP requests from an RPG program.
  • Parse JSON responses to extract useful data.
  • Implement a practical example to fetch and display data from a web service.

Next, you will learn about integrating APIs and handling JSON data in more detail.

© Copyright 2024. All rights reserved