In this section, we will explore how to use REXX for web programming. This includes understanding how to interact with web servers, handle HTTP requests and responses, and parse web content. By the end of this module, you will be able to create simple web applications using REXX.

Key Concepts

  1. HTTP Protocol Basics
  2. Sending HTTP Requests
  3. Handling HTTP Responses
  4. Parsing HTML Content
  5. Creating a Simple Web Server

HTTP Protocol Basics

The Hypertext Transfer Protocol (HTTP) is the foundation of any data exchange on the Web. It is a protocol used for transmitting hypertext requests and information between servers and clients.

Key Points:

  • HTTP Methods: GET, POST, PUT, DELETE, etc.
  • HTTP Status Codes: 200 (OK), 404 (Not Found), 500 (Internal Server Error), etc.
  • Headers: Metadata about the request or response (e.g., Content-Type, User-Agent).

Sending HTTP Requests

To interact with web servers, you need to send HTTP requests. REXX can be used to send these requests and handle the responses.

Example: Sending a GET Request

/* REXX script to send a GET request */
address HTTP
'GET http://example.com'

/* Read the response */
response = ''
do while queued() > 0
  pull line
  response = response || line || '0a'x
end

say response

Explanation:

  • address HTTP: Directs the command to the HTTP environment.
  • GET http://example.com: Sends a GET request to the specified URL.
  • The loop reads the response line by line and concatenates it into a single string.

Handling HTTP Responses

Once you send a request, you need to handle the response. This includes checking the status code and processing the response body.

Example: Handling a Response

/* REXX script to handle HTTP response */
address HTTP
'GET http://example.com'

/* Read the response */
response = ''
do while queued() > 0
  pull line
  response = response || line || '0a'x
end

/* Check for HTTP status code */
if pos('200 OK', response) > 0 then
  say 'Request was successful!'
else
  say 'Request failed with response:'
  say response

Explanation:

  • The script checks if the response contains '200 OK' to determine if the request was successful.

Parsing HTML Content

After receiving an HTML response, you may need to parse it to extract specific information.

Example: Extracting Titles from HTML

/* REXX script to extract titles from HTML */
address HTTP
'GET http://example.com'

/* Read the response */
response = ''
do while queued() > 0
  pull line
  response = response || line || '0a'x
end

/* Extract title */
parse var response '<title>' title '</title>'
say 'Page Title:' title

Explanation:

  • The script uses parse var to extract the content between <title> and </title> tags.

Creating a Simple Web Server

You can also create a simple web server using REXX to handle incoming HTTP requests.

Example: Simple Web Server

/* REXX script to create a simple web server */
address HTTPD
'START 8080'

do forever
  'ACCEPT'
  parse pull request
  say 'Received request:' request

  /* Send a simple HTML response */
  response = 'HTTP/1.1 200 OK' || '0d0a'x
  response = response || 'Content-Type: text/html' || '0d0a0d0a'x
  response = response || '<html><body><h1>Hello, World!</h1></body></html>'
  'SEND' response
end

Explanation:

  • address HTTPD: Directs the command to the HTTPD environment.
  • START 8080: Starts the server on port 8080.
  • ACCEPT: Waits for an incoming request.
  • SEND: Sends the HTTP response.

Practical Exercise

Task:

Create a REXX script that sends a POST request to a web server with some data and handles the response.

Solution:

/* REXX script to send a POST request */
address HTTP
'POST http://example.com/api' 'Content-Type: application/json' '0d0a0d0a'x || '{"key":"value"}'

/* Read the response */
response = ''
do while queued() > 0
  pull line
  response = response || line || '0a'x
end

/* Check for HTTP status code */
if pos('200 OK', response) > 0 then
  say 'POST request was successful!'
else
  say 'POST request failed with response:'
  say response

Explanation:

  • The script sends a POST request with JSON data and checks if the response contains '200 OK'.

Summary

In this section, you learned how to:

  • Understand the basics of the HTTP protocol.
  • Send HTTP requests and handle responses using REXX.
  • Parse HTML content to extract information.
  • Create a simple web server with REXX.

These skills will enable you to build web applications and automate web interactions using REXX. In the next section, we will explore how to create comprehensive REXX applications by combining the concepts learned throughout the course.

© Copyright 2024. All rights reserved