Previously I posted about the simple case of executing an HTTP GET against a URL with querystring parameters and then catching and parsing the JSON return from a REST service. However recently I needed to dig a little deeper into UniBasic based REST service programming. My goal was to interact with Text Messaging services from Manage 2000 UniBasic code.
Nexmo and Twilio, at least, provide extensive examples for interfacing with their service from various environments, but not Multi-Value. In each case they provide handy toolkit wrappers that hide all the actual service details and expose the interface as some sort of object with methods and properties, sometimes coded as a JSON request string.
Unfortunately to implement an interface from within UniData I needed to understand some of the things that their nice little wrappers were hiding. One could imagine the JSON request string posting to the service URL and returning another JSON request string. That is, in fact, NOT what happens.
These services accept HTTP POST requests with form variables and then return JSON responses. So simply creating a GET request with a querystring, like my previous post, will not get the job done. Neither will changing the GET to POST and assuming the querystring variables will be magically transformed into form variables in the fashion of the jQuery AJAX command.
Now the UniBasic Extensions submitRequest command does includes a spot for post data:
* submitRequest(request_handle, time_out, post_data, response_headers, response_data, http_status)
And one is tempted to try to put some type of querystring name=value expression into post_data, but I could not get that to work. If these sites did accept JSON post data, then it would obviously fit into this argument.
What does work for x_www_form_urlencoded requests is adding in form variables to an HTTP POST request using addRequestParameter:
Ret = addRequestParameter(HANDLE, NAME, VALUE, "text/plain")
Using addRequestParameter works for both GET querystring handling and POST form variable handling so you get the nice symmetry that jQuery AJAX command exhibits.
This has the added benefit of encoding the text you are stuffing into parameters so you don't inadvertently corrupt the request.
The UniBasic Extensions HTTP POST pattern involves:
- preparing the context for the HTTP Request using a number of setHTTP... commands
- creating the request
- adding the form variables with addRequestParameter
- submitting the request
So a whole Request transaction might go something like:
* Prepare HTTPS Request
No comments:
Post a Comment