Request body is empty when making a POST request via HTML form

ericgio

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?

Alex

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">

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Set a body for WebClient when making a Post Request

Request body is empty in Post

Django POST request empty form HTML form

request body is empty on post request when using FormData()

Make a Post request with request body from HTML form

Making a post http request of form data to a database with empty/blank fields

Making a POST request to google form

Empty body POST request express

POST Request body Is Null or Empty

Empty body in fetch post request

Angular: Sending POST request with empty request body

@RequestBody gives empty JsonObject when making a POST Request

validated_data is empty when making POST request with Django REST

MongoDB Returns Empty Error Object when Making POST Request

Making a MarkLogic xdmp:http-post() request with x-www-form-urlencoded request body

jQuery .ajax POST request has an empty body when received by Node

Request body is empty when posting form-data

Making a PowerShell POST request if a body param starts with '@'

request(...).then is not a function error when making a POST request

Keep getting undefined body when submitting POST request on a form

req.body is [Object: null prototype] {} and req.files is empty when using multer to handle POST request containing form data

When creating a MERN application, I need to create a POST request from frontend to the backend, but the request body is empty

why is 'body' empty in nodejs express post request?

Axios post request.body is empty object

CORS POST Request Returns Empty Body

Swift POST request sends an empty body

Swift Post Request - JSON Body is empty

Post request body empty - XMLHttpRequest to Express server

Flutter POST request body is empty on server side