Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

79
Views
req.body is an empty object express/nodejs

I have a form with two numeric input fields that I'd like to send to the backend, but req.body is always empty. Here is my form:

<form
    class="mt-3"
    id="myForm"
    enctype="multipart/form-data"
    action="/submit-thing1and2"
  >
    <div class="mb-2">
      <label for="thing1" class="form-label"
        >Thing 1</label
      >
      <input
        type="number"
        class="form-control"
        id="thing1"
        name="thing1"
        required
      />
    </div>
    <div class="mb-2">
      <label for="thing2" class="form-label"
        >Thing 2</label
      >
      <input
        type="number"
        class="form-control"
        id="thing2"
        name="thing2"
        required
      />
    </div>

    <div class="form-group-row mb-3">
      <button id="submitThings" type="submit" class="btn btn-primary">
        Submit Things
      </button>
    </div>
  </form>

I have tried using enctype="application/json", "text/html", and "application/x-www-form-urlencoded", and all of them still return an empty object in req.body.

Here is my post request:

form = document.getElementById("myForm");
form.addEventListener("submit", async (event, arg) => {
  event.preventDefault();

  console.log(event.target.action);
  console.log(event.target);
  let data = new FormData(event.target);
  console.log(data.toString());

  fetch(event.target.action, {
    method: "post",
    body: new FormData(event.target),
  })
   .then((res) => {
     console.log(res.status);
     return res.text();
  })
   .then((data) => {
     console.log(data);
  });
});

And here is my server-side handling of the post request - the console.log works, just shows an empty object:

const express = require("express");
const path = require("path");
const PORT = process.env.PORT || 5000;
const app = express();

app.use(express.static(path.join(__dirname)));

app.use(express.json());

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

app.post("/submit-thing1and2", async (req, res) => {
  console.log("req.body", req.body);
  res.send(req.body);
});
app.get("/", (req, res) => {
  res.sendFile("./html/index.html", { root: __dirname });
});

app.listen(PORT, () => console.log(`Listening on ${PORT}`));
about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Use multer to parse multipart/form-data requests:

app.post("/submit-thing1and2",
  multer().fields([{name: "thing1"}, {name: "thing2"}]),
  async function(req, res) {
    ...
  });
about 4 years ago · Santiago Gelvez Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!