Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

81
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda