0

I am trying ti write a sample POST call in express.

var express = require("express")
 , app = express()
 , server = require('http').createServer(app)
 , bodyParser = require('body-parser');
app.listen(80, function() {
 console.log("server started"); 
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.post("/test", function(req, res) {
 try {
 console.log(req.body);
 res.send("working " + req.body.name); 
 } catch(e) {
 console.log("error in test API" + e.message);
 res.end();
 }
});

But I am unable to access body data on the server. It is empty. Below is postman query.

enter image description here

asked Sep 22, 2017 at 6:35
4
  • What data you are sending with the POST request Commented Sep 22, 2017 at 6:39
  • @RohitAgrawal it is a form data name=abcd. It is there in postman screenshot. Commented Sep 22, 2017 at 6:41
  • Empty object is there {} Commented Sep 22, 2017 at 6:46
  • Do you want to access data passed from a form or data posted in JSON? Commented Sep 22, 2017 at 7:34

2 Answers 2

2

Select raw under body in Postman, not form-data if you want JSON data.

Then enter the data in JSON format. Postman should format it if you have the header set: Content-Type: application/json

Edit:

If you want to parse form-data you can't use body-parser as stated in the readme:

This does not handle multipart bodies, due to their complex and typically large nature. For multipart bodies, you may be interested in the following modules:

  • busboy and connect-busboy
  • multiparty and connect-multiparty
  • formidable
  • multer

Github link

answered Sep 22, 2017 at 6:46
Sign up to request clarification or add additional context in comments.

3 Comments

@Shubham Can you show a picture of Postman when you try sending it like that?
I want to access form-data and I changed content-type to application/json
@Shubham Edited my answer.
1

To read as req.body you're missing the Content-Type : application/json header I think, add it and make the request into a raw json. Or else youre getting a string and cannot directly access it as req.body

eg :

{"name" : "abcd"}

(削除) Update :

If you need Form data use the bodyParser to convert text to json

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

(削除ここまで) Update 2 :

It seems the issue is with multipart/form-data you're using. To handle it you would need a specific framework like multer due to security issues. The below should work.

var multer = require('multer');
var upload = multer() ;
app.post('/test', upload.array(), function (req, res, next) {
 console.log(req.body);
});
answered Sep 22, 2017 at 6:47

3 Comments

@Shubham Try setting extended: true
@KalanaDamel multer will work. I used that in separate project. In this form data there is no multipart data I guess. So why body-parser is not working.
@Shubham multipart/form-data is considered as one type of html encoding, so when file uploads through this became a security issue and because of its complexity of data it was taken out of body parser and it was needed to use a middle-ware to access it.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.