cancel
Showing results for 
Search instead for 
Did you mean: 

Access POST payload in my endpoint built with CAP

former_member604013
Participant
0 Kudos

Hi,

I have exposed an endpoint using CAP as an action:

auth-service.cds

service AuthService {
  action loginAsGuest() returns String;}

auth-service.js

module.exports = (srv) => {
  srv.on("loginAsGuest", (req) => {
    console.log("I want to use here the payload of my POST request");
  });
};

How can I access the payload I send with my POST request to that endpoint? I tried to inspect the request object, but it is so big, that it doesn't fit in the console...

thanks in advance!

View Entire Topic

Hi "Tocayo",

A service should be inmutable and is called with GET method. You can implement the post as an action.

service TestService {
  function calc(a: Integer, b: Integer) returns MyResult;
  action acalc(a: Integer, b: Integer) returns MyResult;
}

The implementation.

module.exports = srv => {
  srv.on('calc', (req) => ({sum: req.data.a + req.data.b, color:'Green'}))
  srv.on('acalc', (req) => ({sum: req.data.a + req.data.b, color:'Green'}))
}

Test

### calc() function
GET http://localhost:4004/nnapi/dns/calc(a=1,b=2)

### calc with post
POST http://localhost:4004/nnapi/dns/acalc
Content-Type: application/json

{  "a": 3, "b":4}

Hope it helps you.