You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Current »

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.

If you are using XPRIM_RandomAccessJsonReader (or XPRIM_JsonReader), you can check the error information returned when you set the source response (using SetSourceHttpResponse method).

The following example builds upon the example from the previous section. Notice that we know declare a component of type XPRIM_ErrorInfo, and pass it when we call the SetSourceHttpResponse method. We then check the OK property of the error info object to check if the JSON reader successfully reads the HTTP response body (which indicates that response is a well-formed JSON string)

      Define_Com Class(#XPRIM_HttpRequest) Name(#Req)
      Define_Com Class(#XPRIM_RandomAccessJsonReader) Name(#Reader)
      Define_Com Class(#XPRIM_ErrorInfo) Name(#ErrorInfo)
      ...
      #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) ErrorInfo(#ErrorInfo)
        If (#ErrorInfo.OK)
             * Read JSON values
             . . .

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