Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

157
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

0

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

about 4 years ago · Santiago Gelvez Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda