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

321
Visualizações
How to post request?

why is the code outputting an empty req.body

const express = require("express");
const app = express();

app.use(express.urlencoded());

app.get("/", (req, res, next) => {
  res.send(`<script>
    fetch("/push", {
        body: "someText",
        headers: {
            "Content-Type": "text/html; charset=UTF-8",
        },
        method: "POST",
    }).then((data) => console.log(data));
</script>`);
});

app.post("/push", function (req, res, next) {
  console.log(req.body); //empty here //{}
  res.send(req.body);
});

app.listen(3000);
over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

Your fetch statement makes a POST request with Content-Type: text/html, which is highly unusual. express.urlencoded() does not fill req.body for this content type, it is only meant for Content-Type: application/x-www-form-urlencoded.

You can parse arbitrary textual content with code like this:

var body = "";
req.on('data', function(chunk) {
  body += chunk.toString();
})
.on('end', function() {
  res.send(body);
});
over 4 years ago · Santiago Trujillo Relatório

0

Just add the express.urlencoded({ extended: true }) also use the express.js

const express = require("express");
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json())

app.get("/", (req, res, next) => {
  res.send(`<script>
    fetch("/push", {
      body: "someText",
      headers: {
        "Content-Type": "text/html; charset=UTF-8",
      },
      method: "POST",
    }).then((data) => console.log(data));
  </script>`);
});

app.post("/push", function (req, res, next) {
  console.log(req.body);
  res.send(req.body);
});

app.listen(3000);
over 4 years ago · Santiago Trujillo 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