Calls a REST service that returns and optionally expects JSON data. The JSON data is returned as a FoxPro simple value, object or collection.
Parameter Inputs
To call a service you specify the HTTP Verb, plus any optional data to send to the service. You can only pass a single value that is serialized to JSON, but this value can be an object that contains many nested sub-objects or collections. REST services in general accept only a single data object which can contain mutliple sub-objects that act as parameters. How this is set up depends on the service however.
Return Values
This method expects the service to return a JSON response (application/json
content type) which is turned into a FoxPro value, object or collection that is returned as part of this method.
Error Results
If the result is an error response (400-599 HTTP Error Codes) you can check lError
and cErrorMsg
. If the error result includes JSON and a .message
or .Message
property that value is set for the error message.
Errors return a Response Value
Note that
CallService()
always returns some sort of response - either a string of the raw HTTP buffer, or if the data isapplication/json
content, the deserialized JSON object. This happens even if an error occurred. So if you get a 401 (Not Authorized) and the server sends a JSON object response with error info, you get that object back.The bottom line is: You always need to check the
.lError
flag or check the `. to ascertain whether the req
o.CallService(lcUrl,lvData,lcVerb)
Parameters
lcUrl
The URL to access the service method on the server
lvData
Optional - A FoxPro value or object or collection that is serialized to JSON and sent to the server
lcVerb
Optional - The HTTP Verb to use for this request (GET,POST,PUT,DELETE,HEAD,OPTIONS etc.). Default is GET if no data is sent or POST if lvData is not null or empty.
Example
loProxy = CREATEOBJECT("wwJsonServiceClient")
lcUrl = "http://albumviewer.west-wind.com/api/album"
lvData = loAlbum && FoxPro object
lcVerb = "PUT" && HTTP Verb
*** Make the service call and returns an Album object
loAlbum = loProxy.CallService(lcUrl,lvData,lcVerb)
IF (loProxy.lError)
? loProxy.cErrorMsg
RETURN
ENDIF
? loAlbum.Title
? loAlbum.Artist.ArtistName
See also:
Class wwJsonServiceClient© West Wind Technologies, 1996-2024 • Updated: 04/18/19
Comment or report problem with topic