Node.js/Express form post req.body not working
Asked Answered
M

3

29

I'm using express and having trouble getting form data from the bodyParser. No matter what I do it always comes up as an empty object. Here is my express generated app.js code (the only thing I added was the app.post route at the bottom):

var express = require('express');

var app = module.exports = express.createServer();

// Configuration

app.configure(function(){
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade');
    app.use(express.bodyParser());
    app.use(express.methodOverride());
    app.use(app.router);
    app.use(express.static(__dirname + '/public'));
});

app.configure('development', function(){
    app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
});

app.configure('production', function(){
    app.use(express.errorHandler()); 
});

// Routes

app.get('/', function(req, res){
    res.sendfile('./public/index.html');
});

app.post('/', function(req, res){
    console.log(req.body);
    res.sendfile('./public/index.html');
});

app.listen(3010);

Here is my HTML form:

<!doctype html>
<html>
  <body>
<form id="myform" action="/" method="post" enctype="application/x-www-form-urlencoded">
  <input type="text" id="mytext" />
  <input type="submit" id="mysubmit" />
</form>
  </body>
</html>

When I submit the form, req.body is an empty object {}

Its worth noting that this happens even if I remove the enctype attribute from the form tag

...Is there something I am missing/doing wrong?

I am using node v0.4.11 and express v2.4.6

Manwell answered 22/9, 2011 at 22:5 Comment(0)
L
41
<form id="myform" action="/" method="post" enctype="application/x-www-form-urlencoded">
  <input type="text" name="I_appear_in_req_body" id="mytext" />
  <input type="submit" id="mysubmit" />
</form>

The body of a HTTP post is a key/value hash of all the form controls with a name attribute, and the value is the value of the control.

You need to give names to all your inputs.

Lewislewisite answered 22/9, 2011 at 22:10 Comment(0)
S
3

It also due to content type. please see console.log(req) object.

'content-type': 'application/json; charset=UTF-8’  // valid.

'content-type': 'application/JSON; charset=UTF-8’  // invalid & req.body would empty object {}.

To check content type by console.log(req.is('json')) // return true/false

I think 'charset=UTF-8' is negligible in above.

Stupefy answered 22/9, 2011 at 22:5 Comment(0)
C
1

If your form looks like this

<form action="/", method="post">
    <label for="for_name">Name: </label>
    <input id="for_name" type="text" name="user_name"/>
    <button type="submit">Submit</button>
</form>

And if you are using the following line of code

app.use(express.json());

then the req.body will be empty because it only parses req with content-type application/json but the default value of request originating from element is application/x-www-form-urlencoded. Hence the following line of code will solve the issue

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

My first StackOverflow contribution. Yay!!

Coaptation answered 26/11, 2022 at 13:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.