Webhook - Set value
This example shows how to create a webhook endpoint secured with an API key that receives HTTP POST requests and maps their payload to OnSphere values.
Create a user with an API key assigned to the
/internal/data-accessgroup.Define webhook endpoints that accept HTTP POST requests and forward the payload.
Map endpoint payload fields to OnSphere values of different types (boolean, numeric, decimal, JSON).
Extract nested JSON fields from the request body and expose them as typed values.
git checkout origin/osp-web-configuration .
git checkout origin/osp-cli-configuration .
git checkout origin/example-webhook-set-value-all-types .
Configuration structure
Steps
Setup a user with an API key
To set a webhook value, the user must be member of the groups
/internal/data-accessmodules/keycloak/keycloak-1/users.keycloak{ "users": [ { "enabled": true, "groups": [ "/internal/data-access" ], "username": "example", "email": "example@localhost", "firstName": "example", "lastName": "example", "credentials": [ { "initial": true, "temporary": true, "type": "password", "value": "mysuperpassword" } ], "attributes": { "authorizedKeys": [], "apiKey": "simple-api-key" } } ] }
This API key will be used to authenticate the emitter of the HTTP POST requests and its user access rights will be applied.
Warning
When defining an API key for a user, it may take up to 1 minute for it to be applied.
Create two webhook endpoints
Configure triggers for boolean value
Configure triggers for json value
root/webhook/text-endpoint/boolean-value/value.ospp{ "name": "Boolean example", "description": "Example of boolean value from json", "type": "BOOLEAN", "retention": "STEADY" }
root/webhook/text-endpoint/decimal-value/value.ospp{ "name": "Decimal example", "description": "Example of decimal value from json", "type": "DECIMAL", "retention": "STEADY" }
root/webhook/text-endpoint/json-value/value.ospp{ "name": "Wind", "description": "Example of extracting a field", "type": "TEXT", "retention": "FIRE_AND_FORGET" }
root/webhook/text-endpoint/json-value/owner.webhook{ "linkedEndpoint": "root.webhook.text-endpoint", "extraction" : { "path": "alert.information.wind", "type": "JSON" } }
Note
The value will attempt to extract the field
alert.information.windfrom the JSON body and make it available as an integer. If the content is not in JSON format or if the content does not contain the expected field, an error will be produced instead.Send an HTTP POST request
Using an HTTP client of your choice, you can now send an HTTP POST request to the webhook and see the result by subscribing on the two values created earlier. The HTTP request should look something like this :
curl -X POST http://stack-1.onsphere.local:5000/osp/webhooks/text \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -H "Authorization: Bearer simple-api-key" \ -d '{ "alert": { "temperature": 30, "temperature-decimal": 3.5, "unit": "degrees", "error": true, "information": { "wind": "strong" } } }'
wget -qO- --method=POST \ --header="Accept: application/json" \ --header="Content-Type: application/json" \ --header="Authorization: Bearer simple-api-key" \ --body-data='{ "alert": { "temperature": 30, "temperature-decimal": 3.5, "unit": "degrees", "error": true, "information": { "wind": "strong" } } }' \ http://stack-1.onsphere.local:5000/osp/webhooks/text
echo '{ "alert": { "temperature": 30, "temperature-decimal": 3.5, "unit": "degrees", "error": true, "information": { "wind": "strong" } } }' \ | http POST http://stack-1.onsphere.local:5000/osp/webhooks/text \ Accept:application/json \ Authorization:"Bearer simple-api-key"
POST http://stack-1.onsphere.local:5000/osp/webhooks/text HTTP/2.0 Accept: application/json Content-Type: application/json Authorization: Bearer simple-api-key { "alert": { "temperature": 30, "temperature-decimal": 3.5, "unit": "degrees", "error": true, "information": { "wind": "strong" } } }
Visualize the change with osp-cli
See OnSphere CLI