Skip to Content
1
Apr 23, 2020 at 10:18 AM

Receive arbitrary payloads with CAP service?

448 Views

Hi experts,

I am wondering how I can make a CAP service receive and process an arbitrary payload? Background: A CAP application should offer a webhook for another application X to submit notifications to. X sends something like a https://cloudevents.io/ payload.

First I tried to model an entity matching the payload I am expecting, but that doesn't work as the payload includes names incompatible with CDS syntax.

I then tried a simple service with one action and a custom implementation. This works as in the request is successfully posted to the action with 204. However, I can't find the payload in my custom implementation.

service EventsService {
    action notify();
}
module.exports = srv => {
    srv.on('notify', async (req) => {
        console.log(req.data);
    })
}

Example payload

{"event-type": "SalesQuote.Root.Updated","event-type-version": "v1","event-id": "00163e87-5338-1eda-a0dd-83d76d55a94a","event-time": "2020-04-20T09:48:41Z",
"data": {"root-entity-id":"00163E8753AA1EDA9CE545168DEE707B","entity-id":"00163E8753AA1EDA9CE545168DEE707B"}}

req.data is empty. This makes sense I suppose, since I modelled the action without any parameters. I looked through req._.req but can't find my payload there either.

I tried payload content types other than JSON but they can't be deserialized.

Is there a way to access arbitrary payload data in a CAP service? Am I off in my approach and is there another pattern to implement a webhook notification?

Best regards,

Manuel