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

609
Visualizações
Node.js React node-fetch empty body post

I am trying to do a simple post from Reactjs to a node.js server using node-fetch; however, the body of my request is always empty (I have not been able to come up with any configuration which results in a non-empty body). I have read seemingly every stackOverflow question about this and none solves my predicament, sadly. Here is my reactjs code:

fetch('http://localhost:3000/sayhello/', {headers: new Headers({
            'Content-Type': 'application/json'
        }), method: 'POST', body: {"nose":"bop"} })
        .then(function(res) {
            return res.json();
        }).then(function(json) {
            console.log(json);
        });

And my server.js: ... import BodyParser from 'body-parser';

app.use(BodyParser.urlencoded({extended : true}));
app.use(BodyParser.json());
app.post('/sayhello/', function(req, res) {
  console.log(req.body);
  res.send(req.body);
});

The console always gives an empty body, and the response is always undefined. This happens too when rather than JSON I just post a string as body. Any ideas?

Thanks

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Not to worry friends it seems I have figured it out. I was missing JSON.stringify in body. I hope this helps someone sometime, so they avoid pulling out as much hair as I did :)

about 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