on 01-15-2020 4:34 AM
Hi,
I am using a query as below.
http://localhost:4004/analytics-output/ApplicationUsageByPeriod?$filter=timestamp gt 2018-09-04T00:55:54Z
I have a custom handler for this call. I wanted to check how query parameters appear, so I did something like this (just for testing).
module.exports = cds.service.impl (srv => {
srv.on('READ','ApplicationUsageByPeriod', (req)=>{
req.reject(409, JSON.stringify(req.data));
// req.reject(409, JSON.stringify(req.query));
})
});
But I got an empty output as below.
<error xmlns="http://docs.oasis-open.org/odata/ns/metadata">
<code>409</code>
<message>{}</message>
</error>
But if I use req.query instead of req.data, I get the following output. This seems alright.
<error xmlns="http://docs.oasis-open.org/odata/ns/metadata">
<code>409</code>
<message>
{"SELECT":{"from":{"ref":["AnalyticsOutput.ApplicationUsageByPeriod"]},"columns":[{"ref":["application"]},{"ref":["applicationName"]},{"ref":["count"]},{"ref":["timestamp"]}],"where":[{"ref":["timestamp"]},">",{"val":"2018-09-04T00:55:54Z"}],"limit":{"rows":{"val":1000}}}}
</message>
</error>
So I am unable to understand why I do not get output when I use req.data. Has anyone encountered this?
Hi Krishna,
req.data should not contain the query options. Actually, we currently do not add them to req object directly, but it is on the roadmap.
In the meantime, you can get them from the original express request object at req._.req.query.
Best,
Sebastian
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think req.data will give the data sent in the payload (i.e. body) of, for example, a POST request, rather than anything else. That to me, without even looking, is what "feels" right with that API name, from other libraries in the same space.
I note that the docu at https://cap.cloud.sap/docs/node.js/api#reqdata--- says "Captures all query parameters as well as http post bodies as a single object" but I'm not sure that's accurate, at least not in my experience - perhaps one of the team can comment here too.
I just tested this too, and got what I expected:
module.exports = srv => {
srv.before('CREATE','Things', req => {
console.log("DATA:", req.data)
console.log("QUERY:", req.query)
})
}
And with this request:
curl -H 'Content-Type: application/json' -d '{"ID": 1, "name":"DJ"}' http://localhost:4004/test/Things
I get this output:
DATA: { ID: 1, name: 'DJ' }
QUERY: { INSERT: { into: 'dj.Things', entries: [ { ID: 1, name: 'DJ' } ] } }
Cheers
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi DJ,
You are right that "Captures all query parameters as well as http post bodies as a single object" is erroneous. Thanks for pointing that out.
Best,
Sebastian
User | Count |
---|---|
90 | |
10 | |
10 | |
10 | |
7 | |
7 | |
6 | |
5 | |
4 | |
3 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.