How to make RESTful web service calls - imona/tutorial GitHub Wiki
There are 4 different script methods for a GET request. Method signatures with parameters and returns types are:
Object callRestServiceViaGet(String url);
Object callRestServiceViaGet(String url, Map<String, ?> urlParameters);
Object callRestServiceViaGet(String url, String userName, String password);
Object callRestServiceViaGet(String url, Map<String, ?> urlParameters, String userName, String password);If urlParameters are given, they will be replaced by their corresponding keys in the url parameter.
The below two methods are the same as the above two, but they use basic authentication with given credentials.
Returns the response as an object.
var result = callRestServiceViaGet(
"http://maps.googleapis.com/maps/api/geocode/json?address={address}&sensor={sensor}",
["address" : "1600+Amphitheatre+Parkway,+Mountain+View,+CA", "sensor" : "false"]);
warn(result.status);
The first parameter to the callRestServiceViaGet method is the endpoint URL for the service. This URL may contain placeholders in it (denoted by {address} and {sensor} in the example), which will be replaced by the corresponding values from the map which you provide as the second argument to the callRestServiceViaGet method. The response from the service is returned as a map. Values in the map can be accessed by their keys as shown in the example.
There are 4 different script methods for a POST request. Method signatures with parameters and returns types are:
Object callRestServiceViaPost(String url, Object request);
Object callRestServiceViaPost(String url, Object request, Map<String, ?> urlParameters);
Object callRestServiceViaPost(String url, Object request, String userName, String password);
Object callRestServiceViaPost(String url, Object request, Map<String, ?> urlParameters, String userName, String password);If urlParameters are given, they will be replaced by their corresponding keys in the url parameter.
request parameter is sent in the body of request. If request does not need a body, it can be left null. Or it can be a map (i.e. tweeting on twitter), or an other type of object.
The below two methods are the same as the above two, but they use basic authentication with given credentials.
Returns the response as an object.
var resp = callRestServiceViaPost("https://www.googleapis.com/urlshortener/v1/url",
["longUrl": "http://www.google.com/"]);
warn(resp); //prints all output parameters in output map
warn(resp.variable1); //prints the variable1 output parameter in response map
callRestServiceViaPost is similar to callRestServiceViaGet. As a difference, it takes the object to be sent to the service as its second parameter. In the example, a request that contains a longUrl field is sent to the service. The response is again in the form of a map, whose values can be reached by using their keys.
An optional third parameter to the callRestServiceViaPost method takes a map for the URL parameters, which are used to parameterize the URL, as is done for the callRestServiceViaGet method.
There are 4 different script methods for a PUT request. Method signatures with parameters and returns types are:
void callRestServiceViaPut(String url, Object request);
void callRestServiceViaPut(String url, Object request, Map<String, ?> urlParameters);
void callRestServiceViaPut(String url, Object request, String userName, String password);
void callRestServiceViaPut(String url, Object request, Map<String, ?> urlParameters,
String userName, String password);If urlParameters are given, they will be replaced by their corresponding keys in the url parameter.
request parameter is sent in the body of request. If request does not need a body, it can be left null. Or it can be a map (i.e. tweeting on twitter), or an other type of object.
The below two methods are the same as the above two, but they use basic authentication with given credentials.
PUT requests do not return anything.
callRestServiceViaPut("https://www.googleapis.com/urlshortener/v1/url",
["longUrl": "http://www.google.com/"]);
warn(resp); //prints all output parameters in output map
warn(resp.variable1); //prints the variable1 output parameter in response map
callRestServiceViaPut is similar to callRestServiceViaGet. As a difference, it takes the object to be sent to the service as its second parameter. In the example, a request that contains a longUrl field is sent to the service. The response is again in the form of a map, whose values can be reached by using their keys.
An optional third parameter to the callRestServiceViaPut method takes a map for the URL parameters, which are used to parameterize the URL, as is done for the callRestServiceViaGet method.
There are 4 different script methods for a DELETE request. Method signatures with parameters and returns types are:
void callRestServiceViaDelete(String url);
void callRestServiceViaDelete(String url, Map<String, ?> urlParameters);
void callRestServiceViaDelete(String url, String userName, String password);
void callRestServiceViaDelete(String url, Map<String, ?> urlParameters, String userName, String password);If urlParameters are given, they will be replaced by their corresponding keys in the url parameter.
request parameter is sent in the body of request. If request does not need a body, it can be left null. Or it can be a map (i.e. tweeting on twitter), or an other type of object.
The below two methods are the same as the above two, but they use basic authentication with given credentials.
DELETE requests do not return anything.
All restful service call methods can use authentication by providing a username and a password as their last two parameters as in the example below:
callRestServiceViaGet('url', urlParametersAsMap, userName, password);