How to access the request body when sending POST using Node.js and Express?

I have the following Node.js code:

var express = require('express'); var app = express.createServer(express.logger()); app.use(express.bodyParser()); app.post('/', function(request, response) { response.write(request.body.user); response.end(); }); 

Now, if I POST something like:

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

I get Someone as expected. Now, if I want to get the body of a full request? I tried doing response.write(request.body) , but Node.js throws an exception saying that "the first argument must be a string or buffer", then goes to an "infinite loop" with an exception that says: "Unable to set headers after sending them. "; this is also true even if I did var reqBody = request.body; and then wrote response.write(reqBody) .

What is the problem?

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

+95
javascript express
Jul 24. 2018-12-12T00:
source share
8 answers

Express 4.0 and up:

$ npm install --save body-parser

And then in your node application:

 const bodyParser = require('body-parser'); app.use(bodyParser); 



Express 3.0 and below:

Try passing this in your cURL call:

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

and make sure your data is in JSON format:

{"user":"someone"}

Alternatively, 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()); app.post('/', function(req, res){ console.dir(req.body); res.send("test"); }); app.listen(3000); 

This other question may also help: How to get JSON in a node.js POST express request?

If you do not want to use bodyParser, check this other question: https://stackoverflow.com/a/167295

+93
Jul 24 2018-12-12T00:
source share

As with Express 4, this code is as follows. Note that you 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); app.post('/update', function(req, res) { console.log(req.body); // the posted data }); 
+38
Apr 24 '15 at 3:18
source share
 var express = require('express'); var bodyParser = require('body-parser'); var app = express(); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()) var port = 9000; app.post('/post/data', function(req, res) { console.log('receiving data...'); console.log('body is ',req.body); res.send(req.body); }); // start the server app.listen(port); console.log('Server started! At http://localhost:' + port); 

This will help you. I assume you send the body to json.

+18
May 10 '17 at 10:11
source share

From express v4.16, you can simply use the JSON middleware :

 app.use(express.json()) 

Like this:

 const express = require('express') app.use(express.json()) // <==== parse request body as JSON app.listen(8080) app.post('/test', (req, res) => { res.json({requestBody: req.body}) // <==== req.body will be a parsed JSON object }) 

Note - body-parser , on which it depends, is already included in the express.

Also don't forget to send the header Content-Type: application/json

+15
Apr 20 '18 at 14:19
source share

Try the following:

 response.write(JSON.stringify(request.body)); 

This will take the object that bodyParser created for you, and return it back to the string and write it back. If you need the exact request body (with the same space, etc.), you will need the data and end listeners attached to the request earlier, and create a piece of the line with a piece, as you can see in json parsing the source code from the connection .

+3
Jul 24 '12 at 13:15
source share

What you claim to be โ€œtrying to doโ€ is exactly what you wrote in code that works โ€œas expectedโ€ when you invoke it with curl.

The error you get does not seem to be related to any code that you showed us.

If you want to receive a raw request, set the handlers to request for the data and end events (and, of course, delete any calls to express.bodyParser() ). Note that data events will occur in chunks, and if you do not set the encoding for the data event, these chunks will be buffers, not strings.

+1
Jul 24 2018-12-12T00:
source share

This can be achieved without depending on the body-parser , listen for request:data and request:end and return a response at the end of the request, see the code example below. ref: https://nodejs.org/en/docs/guides/anatomy-of-an-http-transaction/#request-body

 var express = require('express'); var app = express.createServer(express.logger()); app.post('/', function(request, response) { // push the data to body var body = []; request.on('data', (chunk) => { body.push(chunk); }).on('end', () => { // on end of data, perform necessary action body = Buffer.concat(body).toString(); response.write(request.body.user); response.end(); }); }); 
0
Jul 10 '18 at 16:24
source share

You use the following code to register data:

 router.post("/users",function(req,res){ res.send(JSON.stringify(req.body, null, 4)); }); 
-2
Sep 09 '16 at 4:32
source share



All Articles