Let's start and create an example where we'll retrieve posts from a public REST API, such as JSONPlaceholder which is a free online REST API that you can use for demonstration and testing purposes.
Here's the URL endpoint to retrieve posts: https://jsonplaceholder.typicode.com/posts
Now, let's write an Xbase++ code snippet that would use this URL to make a GET request to retrieve posts and handle the response.
2. Then we get the instance of HttpRequest() from the HttpClient().
3. We set the HTTP method to "GET" since we want to retrieve data.
4. We call the HttpClient():send() method to make the API request and save the response in cResponse.
5. Then we check the status code of the HTTP response using the HttpClient():getStatusCode() method. If the status code is 200 (which means "OK" in HTTP status codes), we convert the JSON response into an array using the Json2Var() function and print it out. If the status code is not 200, we print an error message along with the status code.
This is a very basic example of making a REST API call using Xbase++. There are many other things you can do, such as sending data with a `POST` request, adding custom headers, etc.
Thats it. 30 seconds are gone.
ILX article: Creating a multi-part POST request, eg. for accessing a REST API
Xbase++ documentation: Class HttpClient()
Xbase++ documentation: Class HttpRequestMessage()
Xbase++ documentation: Function Json2Var()
Here's the URL endpoint to retrieve posts: https://jsonplaceholder.typicode.com/posts
Now, let's write an Xbase++ code snippet that would use this URL to make a GET request to retrieve posts and handle the response.
Xbase++:
PROCEDURE Main
LOCAL oClient, oRequest, cResponse, aResponse
// Initialize the HttpClient with the URL
oClient := HttpClient():new( "https://jsonplaceholder.typicode.com/posts" )
// 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
Explanation of the Code:
1. First, we create an instance of HttpClient() and pass in the URL for the posts endpoint of the JSONPlaceholder API.2. Then we get the instance of HttpRequest() from the HttpClient().
3. We set the HTTP method to "GET" since we want to retrieve data.
4. We call the HttpClient():send() method to make the API request and save the response in cResponse.
5. Then we check the status code of the HTTP response using the HttpClient():getStatusCode() method. If the status code is 200 (which means "OK" in HTTP status codes), we convert the JSON response into an array using the Json2Var() function and print it out. If the status code is not 200, we print an error message along with the status code.
This is a very basic example of making a REST API call using Xbase++. There are many other things you can do, such as sending data with a `POST` request, adding custom headers, etc.
Thats it. 30 seconds are gone.
References:
ILX article: A deeper look into REST API calls 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: Function Json2Var()