Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

156
Views
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 answers
Answer question

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 Report

0

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

about 4 years ago · Santiago Gelvez Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!