drewwyatt
drewwyatt

Reputation: 6027

How do I receive post data in Node/Express?

I am having trouble figuring out why sending info to a post request to my app always shows empty (some of this omitted for brevity).

var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');

var app = express();

// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(require('less-middleware')(path.join(__dirname, 'public')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.bodyParser());

app.post('/sendmail', function(req, res) {
    console.log(req.body);
});

http.createServer(app).listen(app.get('port'), function(){
  console.log('Express server listening on port ' + app.get('port'));
});

enter image description here

Posting to that URL logs this in the console:

Express server listening on port 3000
{}

What gives?

Upvotes: 0

Views: 159

Answers (3)

Dmitry Matveev
Dmitry Matveev

Reputation: 5376

This is not exactly the solution you are looking for (I am sorry, don't vote me down just for this one :) ) but I still wanted to share my own experience with postman, node and automated testing frameworks, hope it will at least allow you to carry on with what you are doing.

To my experience there is either something wrong with postman or I am missing a vital bit of information. Let me explain, when I just started using postman, I was able to successfully POST data to my node app, but then suddenly it refused to work as expected ( edit: same problem you are having here... 'body' is arriving with no data inside )... and yes I know the bit about header types and triple checked the settings in postman and what's not.

Having failed to work out what was wrong with this application and not willing to waste any more time I rolled back to using a good-old way of testing using a simple framework with a little help from HTTP library.

A very simple to use combo of mocha with supertest will let you have a bunch of control over what and how you want to test your app and basically simulate completely all aspects of interaction with your app in whichever order you require and so forth...

Upvotes: 1

Ben
Ben

Reputation: 5074

You need to change your client from "Text" to "JSON" in the combobox located above your POST json data

enter image description here

Upvotes: 0

Andrey Sidorov
Andrey Sidorov

Reputation: 25456

bodyParser middleware sets req.body based on POST content and mime type - using JSON.parse if its */json or urldecode if type is */urlencoded. Other body types are not supported. As already commented, you need to set header or set post type in Postman

Upvotes: 1

Related Questions