cancel
Showing results for 
Search instead for 
Did you mean: 

CAP - Reading params from request path

0 Kudos

Request Params is empty for the following request POST /test/123456/data

Any idea how to read the 'id' value 123456 ? Request params is empty.

I do have the following cds:

service RawService @(path:'/tests/:id') @(impl:'./lib/raw-service.js') {
   action data() returns String;
}

the service 'raw-service.js'

srv.on ('data', async (req) => {
    return req.params.id
})
View Entire Topic
johannesvogel
Advisor
Advisor
0 Kudos

Hi Nicolae,

@path specifies the url prefix under which your service will be served. It is not possible to set placeholders here.

Please have a look at the documentation on how to model actions.

https://cap.cloud.sap/docs/cds/cdl#actions

If 123456 is actually a parameter of your action, you can model it like

service RawService @(path:'/tests') @(impl:'./lib/raw-service.js') {
   action data(param:Integer) returns String;
}

and send a request like POST /tests/data with payload {"param": 123456}.

req.data will contain the parameters.

srv.on ('data', async (req) => {
    return req.data.param
})

If that does not help, please provide a bit more context on what you're trying to achieve.

Best regards,

Johannes

0 Kudos

Hi Johannes,

Thank you for your response. The way you described it, I know that is working.

However, I wanted to read the id value from the provided path, like is happening in standard REST.

The interesting part is that CAP doesn't complain in the way I defined(same like I described in the original comment 😞

service RawService @(path:'/tests/:id')@(impl:'./lib/raw-service.js'){
   action data() returns String;
}

srv.on ('data', async (req) => {
    logger.info(`Params: ${JSON.stringify(req.params)}`)
    logger.info(`Data: ${JSON.stringify(req.data)}`)
    logger.info(`Express Params: ${JSON.stringify(req._.req.params)}`)
    return `${req._.req.params.id}`
})

Request:
   POST /tests/123456/data
Response:
  {
    "@odata.context": "$metadata#Edm.String",
    "value": "123456"
  }

Console logs:
  [cds] - POST /tests/123456/data
  {"message":"Params: []","level":"info"}
  {"message":"Data: {}","level":"info"}
  {"message":"Express Params: {\"id\":\"123456\"}","level":"info"}


Workaround is to read over express request: req._.req.params.id

gregorw
Active Contributor
0 Kudos

Hi Nocolae,

as CAP currently mainly focuses on providing OData Services I would think you have to adopt to OData for the moment. Hope that the CAP team will not loose focuse. Because I see as the benefit of OData that you learn the URL Syntax once and can use it for every OData Service. In plain REST services the behaviour might be quite different from API to API.

Best regards
Gregor

johannesvogel
Advisor
Advisor
0 Kudos

Hi Nicolae,

your use case is a plain "express" use case, which we do not support.

The only way to get that information, you already detected as req._.req is the plain express request.

req.params in the cds.Request is targeting the parameters of the URL path with are modeled within CDS and not the service path that is handled by express.

Best regards,

Johannes