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 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.

Info

XPRIM_RandomAccessJsonReader

allows

you

to

read

any

part

of

your

JSON

data,

which

you

can

refer

to

by

name

or

by

index.

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.

XPRIM_JsonReader

should

be

used

when

reading

huge

JSON

data

(such

as

those

returned

by

database

services),

as

it

consumes

very

little

memory

(whereas

XPRIM_RandomAccessJsonReader

use

of

memory

is

linear

to

the

size

of

the

JSON

data).

Most

of

the

time,

you

would

choose

XPRIM_RandomAccessJsonReader

over

XPRIM_JsonReader

as

it

is

much

easier

to

use.

Unless

your

JSON

data

is

really

big,

always

opt

for

XPRIM_RandomAccessJsonReader.

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

Next: Checking for Invalid Responses