Groovy HTTP Scripting API

In the previous pages we have primarily covered examples with the Groovy DI API, which allows viewing and sending commands to devices. As mentioned, Bosch IoT Manager also provides an HTTP Scripting API, which enables operators to incorporate REST calls and work with properties as part of their mass management groovy actions. Here we will dive deeper into this topic. The Groovy HTTP Scripting API allows:

  • Sending and receiving HTTP requests and responses from/to external HTTP endpoints.

  • Pulling useful information and pushing summarized statistics from/to external HTTP endpoints.

REST calls work asynchronously, meaning that the actual processing of the response must be executed in a closure.

HTTP Client binding in a Groovy script is possible via the main interface - httpClient or hc.

The following example illustrates the scheme on how to perform a GET request to an external HTTP endpoint. Go through the comments for better understanding:

// define the basic HTTP request
def httpRequest = httpClient.buildHttpRequest(HttpMethod.GET, 'https://postman-echo.com/get?foo1=bar1&foo2=bar2');
// The request can include whatever is needed for your scenario such as:
// httpRequest.putHeader('Content-Type','application/json')
// httpRequest.timeout(6000)
// etc...
// then you need to define the async processing of the response
Closure processResponse = {result,error ->
if (error != null) {
throw new Exception(error)
}
HttpResponse res = result as HttpResponse
return 'http response code is: ' + res.statusCode()
}
// you can send the request with a response handling callback
httpRequest.send(processResponse)

Be aware that, for security reasons, not all HTTP endpoints are whitelisted and you may not be able to send requests or receive responses to/from them. The endpoint featured in the example ('https://postman-echo.com/get?foo1=bar1&foo2=bar2') is whitelisted and can be used for testing purposes. However, if you need a specific endpoint to be whitelisted please contact our support team.

REST calls have a separate partial execution element, which can be used for monitoring of the requests, as part of the task or rule. This is important as it is possible that the REST call has finished with and error due to NOT-whitelisting the endpoint, while the whole script can still have a successful execution.

Find more detailed information and all related methods in our Groovy APIs documentation.