Webhook - Read values via GET
This example shows how to expose a webhook endpoint that writes a value with POST, reads it back
with GET, and resets it with DELETE — turning the endpoint into a small inbound REST resource.
Create a user with an API key assigned to the
/internal/data-accessgroup.Define a webhook endpoint declaring
POST,GETandDELETEin itsmethods.Link a
STEADYvalue to the endpoint withowner.webhookso it can be read back.
git checkout origin/osp-web-configuration .
git checkout origin/example-webhook-read-reset .
How it works
The endpoint declares
POST,GETandDELETEin itsmethodslist. A request using a verb that is not declared is rejected with a405 Method Not Allowed.A
POSTwrites the request body into the value linked to the endpoint by itsowner.webhookfile.A
GET(without a linked script) returns a JSON array of the values exposed by the endpoint, each as{ "itemId", "content", "type" }.A
DELETEresets every exposed value to its initialized (unset) state; a subsequentGETthen returns anullcontent.The value uses the
STEADYretention so its last written state is retained and can be read back. AFIRE_AND_FORGETvalue would always read back asnull.
See reading values for the full reference.
Steps
Setup a user with an API key
To call the webhook, the user must be a member of the group
/internal/data-access.modules/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" } } ] }
Warning
When defining an API key for a user, it may take up to 1 minute for it to be applied.
Create the webhook endpoint
The endpoint declares
POST,GETandDELETEin itsmethodslist.root/webhook/state-endpoint/endpoint.webhook{ "moduleId": "modules.web.web-1", "path": "/state", "methods": [ { "verb": "POST" }, { "verb": "GET" }, { "verb": "DELETE" } ] }
Link a value to the endpoint
The
owner.webhookfile links the value to the endpoint; the value itself is configured separately and uses theSTEADYretention so it can be read back.root/webhook/state-endpoint/state-value/owner.webhook{ "linkedEndpoint": "root.webhook.state-endpoint" }
root/webhook/state-endpoint/state-value/value.ospp{ "name": "State", "description": "A value written via POST, read back via GET and reset via DELETE", "type": "TEXT", "retention": "STEADY" }
Write, read and reset the value
Write a value with a
POST:curl -X POST http://stack-1.onsphere.local:5000/osp/webhooks/state \ -H "Authorization: Bearer simple-api-key" \ -H "Content-Type: text/plain" \ -d 'hello'
wget -qO- --method=POST \ --header="Authorization: Bearer simple-api-key" \ --header="Content-Type: text/plain" \ --body-data='hello' \ http://stack-1.onsphere.local:5000/osp/webhooks/state
echo 'hello' \ | http POST http://stack-1.onsphere.local:5000/osp/webhooks/state \ Authorization:"Bearer simple-api-key" \ Content-Type:text/plain
POST http://stack-1.onsphere.local:5000/osp/webhooks/state HTTP/2.0 Authorization: Bearer simple-api-key Content-Type: text/plain hello
Read it back with a
GET:curl http://stack-1.onsphere.local:5000/osp/webhooks/state \ -H "Authorization: Bearer simple-api-key" \ -H "Accept: application/json"
wget -qO- \ --header="Authorization: Bearer simple-api-key" \ --header="Accept: application/json" \ http://stack-1.onsphere.local:5000/osp/webhooks/state
http GET http://stack-1.onsphere.local:5000/osp/webhooks/state \ Authorization:"Bearer simple-api-key" \ Accept:application/json
GET http://stack-1.onsphere.local:5000/osp/webhooks/state HTTP/2.0 Authorization: Bearer simple-api-key Accept: application/json
The endpoint answers with
200and the current value:[ { "itemId": "root.webhook.state-endpoint.state-value", "content": "hello", "type": "TEXT" } ]
Reset it with a
DELETE; a subsequentGETthen returns anullcontent:[ { "itemId": "root.webhook.state-endpoint.state-value", "content": null, "type": "TEXT" } ]