Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Reverted from v. 5

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 PRIMXPRIM_SRVM.HttpClientRequestHttpRequest object).

  • AsString (get the response body as a raw string)
  • AsJson
  • AsFile (
  • get
  • place the response body
  • as JSON document
  • in a file and get the file path)
  • AsJsonObject (get the response body as JSON object)
  • AsJsonArray (get the response body as JSON array
  • )AsJsonReader (get the response body as PRIM_JSON.Reader
  • )

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.Readereither XPRIM_RandomAccessJsonReader or XPRIM_JsonReader.

Info

PRIMXPRIM_JSON.DocumentRandomAccessJsonReader allows you to read any part of your JSON data, which you can refer to by name or by index.
PRIM_JSON.Reader XPRIM_JsonReader is a fast, forward-only reader. It reads the JSON data the way it reads data on a tape. Your JSON data is read sequentially, so you can't directly access different areas of your JSON data. PRIMXPRIM_JSON.ReaderrJsonReader should be used when reading huge JSON data (such as those returned by database services), as it consumes very little memory (whereas PRIMXPRIM_JSON.DocumentRandomAccessJsonReader use of memory is linear to the size of the JSON data).

Most of the time, you would choose PRIMXPRIM_JSON.Document RandomAccessJsonReader over PRIMXPRIM_JSON.ReaderJsonReader as it is much easier to use. Unless your JSON data is really big, always opt for PRIMXPRIM_JSON.DocumentRandomAccessJsonReader.

Example - Reading the Response Body as JSON

...

     {
  "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#XPRIM_JSON.DocumentHttpRequest) Name(#JsonDoc) Reference(*DYNAMIC#Req)
Define_Com Class(#PRIM#XPRIM_JSON.ObjectRandomAccessJsonReader) Name(#JsonLocation) Reference(*DYNAMIC#Reader)
...
   #Req#Req.DoGet Url('https://maps.googleapis.com/maps/api/geocode/json?...')

* Check if request is successful

     If (#Req.Response.IsSuccessHttpStatusCode)

    * Place Set the response into PRIM_JSON.DocumentJSON reader source fot response from the HTTP request

        #Reader.SetSourceHttpResponse HttpResponse(#Req.Response.AsJson Result(#JsonDoc)

    * Place Navigate to the first location response into PRIM_JSON.Object

...

'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

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