0

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?

2 Answers 2

1

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'})
    });
})
Sign up to request clarification or add additional context in comments.

Comments

0

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);
  });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.