on ‎2021 Jun 14 12:41 PM
Hi everyone, greetings!
I have been trying to send out a custom response for the CREATE request on some entity.
Our customer has a requirement where the following events happen in sequence.
1- call CREATE entity from a REST client.
2- We intercept the request after the data is written to the DB table.
3- We create a payload for a remote service from the data.
4- Call the remote service and get a response.
5- Map the response with some additional data to create a custom response.
6- Send the response as a reply to the CREATE request in 1.
I am aware from the docs that any async changes must be done in the ON handler.
srv.on('CREATE', 'Header', async (each, next) => {
generic_result = await next();
generic_result.field = "Some changed value" //Changes reply value
each.data.field = "Some changed value" //Same effect as above
//Call the remote API
//Map the response to the API response
each.data = { custom API response } ( Doesn't work ) OR
return each.reply('{ "custom" : "reply" }') ( Doesn't work ) OR
return each.error (409,`Error`); ( Works with the respective error message )
return each;
}
I have two questions here.
First ( Obvious ) - How can I send a custom reply ( which is not an error ) on a defined enitity definition
OR if that's not possible How can I implement the required flow.
Second ( Low prio ) - In
generic_result = await next(); the reference generic_result and each.data behave like there aare pointing to the same thing. Any change is reflected in the response data of the entity.
Thank you in advance.
Request clarification before answering.
Hi Sasank,
the response to a create request has to match the Entity you defined. The response of your CREATE handler for the "Header" entity has to match the elements defined for Header. You can't respond with an Object with different attributes.
You can write your Header object to the database, then query an external service and update attributes of your Header object with data you received from the external service and return the updated object.
If you want to have an endpoint that receives data, writes it to the database of the CAP application and responds with something else, you should look into creating a custom Express endpoint in your CAP application.
Kind regards,
Sebastian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Create your custom server.js file under srv folder, create a custom express.js app in this server. and call this server.js file on bootstrap. then you will be able to use express library in cap handler.
it is easy. that is why cap is famous where you not only can use beauty of CAP framework but also all functionalities of express.js.
If you implement this, you can handle any payload. No entities are required nothing is required only define an action in service definition and write handler in srv file.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 13 | |
| 7 | |
| 6 | |
| 5 | |
| 4 | |
| 3 | |
| 3 | |
| 3 | |
| 3 | |
| 3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.