Bosch IoT Manager

Groovy HTTP Scripting API example

This page provides a real-life example of a rule which demonstrates a simple use case of our Groovy HTTP Scripting API.

The purpose of the action script is to send a GET request to an external endpoint and report the response as part of the rule's partial execution status.

Create the rule

You can learn the full rule creation possibilities from our Create a rule page. In this case you need to:

  1. Click Create Management Rule.

  2. Type in a user-friendly name and the Groovy script you intend to execute.
    images/confluence/download/attachments/1824820957/dm_HTTPAPI_example_addScript.png
    You can directly copy the Groovy script we use in this example:

    // 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...(see Java API documentation)
    // 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
    // you can add a DI method and execute a command on a device
    return 'http response code is: ' + res.statusCode()
    }
    // you can send the request with a response handling callback
    httpRequest.send(processResponse)
  3. To achieve the desired outcome, you need to execute the rule's groovy action script in the context of device scope. Select Device Scope but check the number of involved devices, to prevent unnecessary executions. The optimal approach would be to use Id Selection scope and pick just one device.

  4. For simplicity, add a Manual Fire trigger.

    images/confluence/download/attachments/1824820957/dm_HTTPAPI_example_addManualTrigger.png
  5. No specific execution options are required.

  6. Click Finish and enable the rule.

    images/confluence/download/attachments/1824820957/dm_HTTPAPI_example_enableRule.png
  7. Fire the rule.

    images/confluence/download/attachments/1824820957/dm_HTTPAPI_example_fireRule.png

See the result

To observe the result of your execution in detail:

  1. Once the value of Finished with success has increased, you can click on the link.

    images/confluence/download/attachments/1824820957/dm_HTTPAPI_example_fireRule2.png
  2. This will open up the Execution Status window, where you need to expand the rule and task to observe the partial executions.

    images/confluence/download/attachments/1824820957/dm_HTTPAPI_example_partialExec.png


    These partial execution statuses represent:

    • The execution state of the whole script.

    • The state of the specific REST call.

    • The state of the Closure. Here under Result Value we see the response we programmed in case of success and we see that the code is 200.

Visit our Java API documentation for more technical information.