The response body contains the information you are after. For example, when you execute an HTTP request to get a geographic coordinate from an address using Google Maps, the resulting coordinate will be returned in the response body.
You can read the response body using one of the read methods of the Response object (of XPRIM_HttpRequest object).
- AsString (get the response body as a raw string)
- AsFile (place the response body in a file and get the file path)
- AsJsonObject (get the response body as JSON object)
- AsJsonArray (get the response body as JSON array)
Most modern web services, especially REST web services, would return the response as JSON. In the past, XML was the prevalent format, however XML seems to have fallen out of favour, and it's very uncommon to see a service that takes/returns XML data.
To efficiently read a JSON response, use either XPRIM_RandomAccessJsonReader or XPRIM_JsonReader.
Example - Reading the Response Body as JSON
When you execute a Google Maps geocoding request, you will get a response much like this:
{
"results" :
[
{
"formatted_address" : "1600 Amphitheatre Parkway, Mountain View, CA 94043, USA",
"geometry" :
{
"location" :
{
"lat" : 37.4224764,
"lng" : -122.0842499
}
}
]
}
We are interested in the lat and long values, which can be accessed via results (element 1) >> geometry >> location.
Define_Com Class(#XPRIM_HttpRequest) Name(#Req)
Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#Reader)
...
#Req.DoGet Url('https://maps.googleapis.com/maps/api/geocode/json?...')
* Check if request is successful
If (#Req.Response.IsSuccessHttpStatusCode)
* Set the JSON reader source fot response from the HTTP request
#Reader.SetSourceHttpResponse HttpResponse(#Req.Response)
* Navigate to the 'location' object (containing the 'lat' and 'lng' values)
* We'll specify a navigation path to navigate to the 'location' element
* Names and indexes in a path are separated by forward slashes
#Reader.BeginObjectWithPath Path('results/1/geometry/location')
* Get the latitude and longitude value
#Latitude := #Reader.ReadNumberWithName('lat')
#Longitude := #Reader.ReadNumberWithName('lng')
* Close "BeginObject" with "EndObject"
#Reader.EndObject
Endif