In this article we will have a deeper look into how to implement different types of REST APIs using Xbase++ 2.0. We assume that a user management REST API exposes two endpoints.:

1. "POST /users" - This creates a new user. For this request, we need to send user data in the request body. The API expects this data in JSON format.
2. "GET /users/{id}" - This retrieves the details of an existing user by the user's ID.

Let's walk through examples of how you might interact with these two endpoints in Xbase++.

1. POST /users​

This is how we might create a new user by making a POST request to the "/users" endpoint:

Xbase++:
PROCEDURE Main
   LOCAL oClient, oRequest, cResponse, aResponse, oUser

   // Initialize the HttpClient with the URL
   oClient := HttpClient():new( "https://example.com/users" )

   // Get the HttpRequest instance
   oRequest := oClient:HttpRequest

   // Set the HTTP method
   oRequest:setMethod( "POST" )

   // Set the content type
   oRequest:setContentType( "application/json" )

   // Prepare user data with a DataObject
   oUser := DataObject():new()
   oUser:name := "John Doe"
   oUser:email := "john.doe@example.com"

   // Convert the DataObject to JSON and send it as the request body
   oRequest:setContent( Var2Json(oUser) )

   // Send the request and get the response
   cResponse := oClient:send()

   // Check the status code
   IF oClient:getStatuscode() == 201
      ? "User created successfully"
   ELSE
      ? "Error:", oClient:getStatuscode()
   ENDIF
RETURN
In this example, we set the HTTP method to "POST" and the content type to "application/json". Then we create a user data object and convert it to JSON using Var2Json(). We send this JSON data as the content of the request. After sending the request, we check the HTTP status code. If it's 201, the user was created successfully.

2. GET /users/{id}​

This is how we might retrieve a user by making a GET request to the "/users/{id}" endpoint:
Xbase++:
PROCEDURE Main
   LOCAL oClient, oRequest, cResponse, aResponse, cUserId

   // ID of the user to retrieve
   cUserId := "123"

   // Initialize the HttpClient with the URL
   oClient := HttpClient():new( "https://example.com/users/" + cUserId )

   // Get the HttpRequest instance
   oRequest := oClient:HttpRequest

   // Set the HTTP method
   oRequest:setMethod( "GET" )

   // Send the request and get the response
   cResponse := oClient:send()

   // Check the status code
   IF oClient:getStatuscode() == 200
      // Convert the response from JSON to an array
      aResponse := Json2Var( cResponse )
 
      // Print the response
      ? aResponse
   ELSE
      ? "Error:", oClient:getStatuscode()
   ENDIF
RETURN
In this example, we replace "{id}" in the URL with the actual ID of the user we want to retrieve and set the HTTP method to "GET". Then we send the request and check the HTTP status code. If it's 200, the user was retrieved successfully, and we print out the user data. If the status code is not 200, we print an error message.

Remember to replace "https://example.com" with your actual API base URL. Please note that error handling here is very basic for demonstration purposes. In a real application, you would likely want to implement more robust error handling.

Hope you enjoyed that little deeper explanation.

References:​

ILX articls: Your first REST Call in 30 seconds using Xbase++
ILX article: Creating a multi-part POST request, eg. for accessing a REST API
Xbase++ documentation: Class HttpClient()
Xbase++ documentation: Class HttpRequestMessage()
Xbase++ documentation: Class DataObject()
Xbase++ documentation: Function Json2Var()