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 PRIM_SRVM.HttpClientRequest object).
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 the PRIM_JSON.Reader.
PRIM_JSON.Document allows you to read any part of your JSON data, which you can refer to by name or by index. |
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(#PRIM_SRVM.HttpClientRequest) Name(#Req)
Define_Com Class(#PRIM_JSON.Document) Name(#JsonDoc) Reference(*DYNAMIC)
Define_Com Class(#PRIM_JSON.Object) Name(#JsonLocation) Reference(*DYNAMIC)
...
#Req.DoGet Url('https://maps.googleapis.com/maps/api/geocode/json?...')
* Check if request is successful
If (#Req.Response.IsSuccessHttpStatusCode)
* Place the response into PRIM_JSON.Document
#Req.Response.AsJson Result(#JsonDoc)
* Place the first location response into PRIM_JSON.Object
#JsonLocation <= #JsonDoc.RootNode<'results'>.ItemAt<1>.Item<'geometry'>.Item<'location'>.AsObject
#Latitude := #JsonLocation.Item<'lat'>.AsDecimal
#Longitude := #JsonLocation.Item<'lng'>.AsDecimal
Endif
A normal practice would be to iterate over the results.
Define_Com Class(#PRIM_SRVM.HttpClientRequest) Name(#Req)
Define_Com Class(#PRIM_JSON.Document) Name(#JsonDoc) Reference(*DYNAMIC)
Define_Com Class(#PRIM_JSON.Object) Name(#JsonLocation) Reference(*DYNAMIC)
...
#Req.DoGet Url('https://maps.googleapis.com/maps/api/geocode/json?...')
* Check if request is successful
If (#Req.Response.IsSuccessHttpStatusCode)
* Place the response into PRIM_JSON.Document
#Req.Response.AsJson Result(#JsonDoc)
For Each(#Result) In(#JsonDoc.RootNode<'results'>)
#JsonLocation <= #Result.Item<'geometry'>.Item<'location'>.AsObject
#Latitude := #JsonLocation.Item<'lat'>.AsDecimal
#Longitude := #JsonLocation.Item<'lng'>.AsDecimal
Endfor
Endif
Next: Checking for Invalid Responses