The request body is empty when executing a POST request through an HTML form

I have a problem with symptoms similar to this question and several others, although the answers did not help me. I am trying to send a password using a simple HTML form to a Node application, but the request body continues to return.

Server:

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

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

the 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 twist 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?

+4
source share
1 answer

The problem is in your form

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

There is inputno element in yourname

Must be

<input name="password" type="password" id="password">
+6
source

Source: https://habr.com/ru/post/1666125/


All Articles