Reputation: 1697
Where in the request object is the json?
For example I know I can use body-parser to do this
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.post('/', function(req, res){
console.log(req.body)
res.json({ message: 'goodbye'})
})
And start my server and hit it with
curl -H "Cont/json" -X POST -d '{"username":"xyz"}' http://localhost:3000/
but is there a way to do it without the body parser includes? Can I just see the json in the request?
Upvotes: 0
Views: 832
Reputation: 48346
You could do it through node stream
as below
app.post('/', function(req, res){
var body = "";
req.on("data", function (data) {
body += data;
});
req.on("end", function() {
console.log(JSON.parse(body));
res.json({ message: 'goodbye'})
});
})
Upvotes: 1
Reputation: 1035
Yep, you can
//pipe to any writable stream
req.pipe(process.stdout);
Sure if u want - u may save it to string using something like this
var tmpstr = "";
req.on("data", function (data) {
tmpstr += data;
});
req.on("end", function() {
//do stuff
console.log("\ndata length is: %d", tmpstr.length);
});
Upvotes: 0