Bosch IoT Device Management - will be discontinued by mid 2024

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. Open Rules from the left navigation on the Bosch IoT Suite UI.

  2. Click the + icon on the right of the header bar.

    images/confluence/download/attachments/2417273350/dm_examples_httpGroovy_createRule.png
  3. Type in a user-friendly name.

  4. To achieve the desired outcome, you need to execute the rule's groovy action script in the context of device scope. In the Type field select Device.

  5. The optimal approach to prevent unnecessary executions would be to use Device selection by ID. Select it and pick just one device from the Select devices blade that opens from the right.

  6. No specific Execution options are required.

  7. Click Next.

    images/confluence/download/attachments/2417273350/dm_examples_httpGroovy_createRule2.png
  8. Click the + icon in the Triggers blade.

  9. For simplicity, add a Manual Fire trigger and click Save. Again, proceed by clicking Next.

    images/confluence/download/attachments/2417273350/dm_examples_httpGroovy_createRule3.png
  10. On the Action step enter the Groovy script you intend to execute and click Finish.
    images/confluence/download/attachments/2417273350/dm_examples_httpGroovy_createRule4.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)
  11. Click Enable rule.

    images/confluence/download/attachments/2417273350/dm_examples_httpGroovy_createRule5.png



  12. Fire the rule.

    images/confluence/download/attachments/2417273350/dm_examples_httpGroovy_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/2417273350/dm_examples_httpGroovy_finishedSuccess.png
  2. This will open up the Executions UI feature, where you need to expand the execution to observe the partial executions.

    images/confluence/download/attachments/2417273350/dm_examples_httpGroovy_executions.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.

  3. Furthermore you can open each partial execution to observe more details about it. Opening the last partial execution status i.e. the Closure one, will show you the response value we programmed in case of success. We can see that the code is 200:

    images/confluence/download/attachments/2417273350/dm_examples_httpGroovy_executionValue.png


Visit our Java API documentation for more technical information.