13

I'm having a problem with symptoms similar to this question and several others, though the answers haven't helped me. I'm trying to submit a password via a simple HTML form to a Node app, but the request body keeps coming back empty.

Server:

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

router.post('/login', (req, res) => {
  console.log(req.body);
  console.log(req.headers['content-type']);
});

Form:

<form action="/login" method="post">
  <input type="password" id="password">
  <button type="submit">Log In</button>
</form>

If I submit the form, I get the following:

{} // req.body
'application/x-www-form-urlencoded' // req.headers['content-type']

However, if I try to curl the endpoint, I get a non-empty req.body:

$ curl -X POST localhost:5000/login -d 'password=testpw'

// Output
{ password: 'testpw' }
'application/x-www-form-urlencoded'

What am I doing wrong?

Community
  • 1
  • 1
ericgio
  • 3,094
  • 4
  • 25
  • 44

2 Answers2

31

Problem is in your form

<form action="/login" method="post">
  <input type="password" id="password">
  <button type="submit">Log In</button>
</form>

Your input doesn't have a name element

Should be

<input name="password" type="password" id="password">
Alex
  • 37,502
  • 51
  • 204
  • 332
5

If your form is properly setup as shown in the accepted answer, but your request body is still empty on your backend, you should check to make sure your backend is using some sort of body parser. If you are using express.js, an example fix is to simply add:

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({limit: '5000mb', extended: true, parameterLimit: 100000000000}));
Big Sam
  • 1,290
  • 14
  • 7