cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Custom response in CREATE/READ handler of entity

2,082

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. 

Accepted Solutions (1)

Accepted Solutions (1)

SebastianEsch
Active Participant
0 Likes

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

0 Likes

Thank you, Sebastian

I was hoping the CAP framework provides an interface for the same,

but yes we were also having the same idea as an alternative solution.

Thank you for confirming the same. I will try to implement a custom endpoint

and use the entities to update in the custom handler.

Answers (1)

Answers (1)

CAPMDeveloper00
Explorer
0 Likes

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.