Consider the following HTTP POST
call to a Node.js server:
curl -H "Content-Type: application/json" \
-X POST \
-d '{"jsonKey":"jsonValue"}' \
'http://localhost:8080?abcd=efgh'
I would like to access both the URL parameters and the JSON payload of the POST request.
Accessing the URL params is pretty straightforward by importing url.parse
:
var server = http.createServer(function(req, res) {
// Parse the params - prints "{ abcd: 'efgh' }"
var URLParams = url.parse(req.url, true).query;
console.log(URLParams);
// How do I access the JSON payload as an object?
}
But how do I access the JSON payload, using native Node.js library (without any npm
import)?
What have I tried
- Printed
req
toconsole.log
, but did not find the POST object - Read the documentation of
req
, which is of typehttp.IncomingMessage
via Adam Matan
No comments:
Post a Comment