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

160
Visualizações
Check if req.body is empty doesnt work with express

I tried to test my login request in postman with an empty body but this check doesn't work. It goes beyond and execute the rest of the code. Why ? Is there another way to check if body is empty ?

route.post("/login", (req, res) => {
  console.log(req.body);
  if (!req.body) {
    console.log("I am here");
    res.status(400).send({ message: "Content cannot be empty" });
    return;
  }
... // check password etc
}
about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You can check how many keys req.body has

if (Object.keys(req.body).length === 0) {
    console.log("I am here");
    res.status(400).send({ message: "Content cannot be empty" });
    return;
}
about 4 years ago · Santiago Gelvez Relatório

0

You can check the length of the Body :if (Objects.keys(req.body).length === 0) {

about 4 years ago · Santiago Gelvez Relatório

0

You can use:

if(Object.keys(req.body).length === 0)

or Object.getOwnPropertyNames(req.body).length == 0

then add your logic.

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