If you expect to see a JSON response from your HTTP request, it's recommended that you always verify that the HTTP response is indeed a well-formed JSON.

Here is an example checking that JSON has been loaded into the JSON Document:

     Define_Com Class(#PRIM_SRVM.HttpClientRequest) Name(#Req)
Define_Com Class(#PRIM_JSON.Document) Name(#JsonDoc) 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)
If_Ref Com(#JsonDoc) Is_Not(*NULL)

              * Good response.  JsonDoc is JSON

       else

              * Bad response.  JsonDoc isn't valid

       endif

Similar code can be used to validate if the structure of the document is correct.  So, before the For/Endfor:

     If_Ref Com(#JsonDoc.RootNode<'results'>) Is_Not(*NULL)
For Each(#Result) In(#JsonDoc.RootNode<'results'>)
Endfor
Endif

Next: Re-using the XPRIM_HttpRequest Object

  • No labels