how to access the request body when posting using node js and express

how to access the request body when posting using node js and express  using -'javascript,node.js,express'

I have the following Node.js code:

var express = require('express');
var app = express.createServer(express.logger());
app.use(express.bodyParser());'/', function(request, response) {

Now if I POST something like:

curl -d user=Someone -H Accept:application/json --url http://localhost:5000

I get Someone as expected. Now, what if I want to get the full request body? I tried doing response.write(request.body) but Node.js throws an exception saying "first argument must be a string or Buffer" then goes to an "infinite loop" with an exception that says "Can't set headers after they are sent."; this also true even if I did var reqBody = request.body; and then writing response.write(reqBody).

What's the issue here?

Also, can I just get the raw request without using express.bodyParser()?

asked Sep 22, 2015 by kotmus2002
0 votes

4 Answers

0 votes

Try passing this in your cURL call:

--header "Content-Type: application/json"

and making sure your data is in JSON format:


Also, you can use console.dir in your node.js code to see the data inside the object as in the following example:

var express = require('express');
var app = express.createServer();

app.use(express.bodyParser());'/', function(req, res){


This other question might also help: How to receive JSON in express node.js POST request?

If you don't want to use the bodyParser check out this other question:

answered Sep 22, 2015 by rolvyrf
0 votes

What you claim to have "tried doing" is exactly what you wrote in the code that works "as expected" when you invoke it with curl.

The error you're getting doesn't appear to be related to any of the code you've shown us.

If you want to get the raw request, set handlers on request for the data and end events (and, of course, remove any invocations of express.bodyParser()). Note that the data events will occur in chunks, and that unless you set an encoding for the data event those chunks will be buffers, not strings.

answered Sep 22, 2015 by manju bhargava
0 votes

As of Express 4, the following code appears to do the trick. Note that you'll need to install body-parser using npm.

var express = require('express');
var bodyParser = require('body-parser');
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));

app.listen(8888);'/update', function(req, res) {
    console.log(req.body); // the posted data
answered Sep 22, 2015 by rajeshujade
0 votes

Try this:


That will take the object which bodyParser has created for you and turn it back into a string and write it to the response. If you want the exact request body (with the same whitespace, etc), you will need data and end listeners attached to the request before and build up the string chunk by chunk as you can see in the json parsing source code from connect.

answered Sep 22, 2015 by suyesh.lokhande