How to Make RESTful API Calls


Xbasic offers quite a number of functions and methods for using the HTTP protocol to make various requests from web servers. Sending a RESTful request to a web service is becoming increasingly popular, but is not always as straightforward a s requesting a simple resource from a web server.


REST (representational state transfer) is a general architectural concept, and specific REST APIs will vary from web service to web service, so there is no "one size fits all" solution. However, the Xbasic HTTP functionality still offers all of the requisite components. The HTTP method used by the API will define how you send any required information to the service, and therefore dictate which Xbasic function to use.

GET is very common because it allows you to simply send data as part of a query string. The My Movie API is a good example of this. To search IMDB for the movie titled "Casablanca", a simple GET request needs to be sent to

The easiest way to do this would be by using http_get(), though http_get_page2() and http_fetch() will also work.

For example:

dim MyResponse as p
MyResponse = http_get("")

The response body will be in the MyResponse.body variable. In the above case, the response is JSON data, so you can then use the json_parse() function to parse the response values.

PUT and POST are typically used when more information is being sent to the web service and generally to be stored there. http_put() and http_post() are the simplest functions to use here:

dim Response as p
Response = http_put(<url>,<put_body>)

However, the limitation of both http_put() and http_post() is that they will always use application/x-www-form-urlencoded as the body's content type. If the service to be accessed requires a body in a different format, http_fetch() must be used so that the alternate content type may be specified.

For example:

dim Response as p
dim Request as p
Request.Host = "<server>"
Request.Page = "<page>"
Request.Header = "Content-Type: application/json"
Request.Body = "<JSON formatted body>"
Response = http_fetch(Request)

DELETE generally operates the same as GET, just taking data in a query string. In these cases, http_delete() may be used just like http_get() would be.