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

212
Views
express req.body is returing an empty json object

I have read all the other posts and I have verified none of the solutions on any of them solve my problem. In insomnia I am setting the content type header to "application/json" and I am applying the express.json() middle ware before any of my routes. The MOST frustrating thing is the body isnt empty in my /register route however it always is inside my /login route. I have even tried putting the middle ware directly into the route with "router.get('/', express.json(), async (req, res) => {" but it was to no avail. Also when I make the route a post I get a "cannot GET /register" with code 404 but not when its a "get" route. On my register route there is both a get and post route so I cant fathom why its any different with the login route ESPECIALLY since I made the register route by copy and pasting the login routes js file. Here is the code for the app

const express = require("express"),
  bodyParser = require('body-parser'),
  rta = require('./RTAuth.js'),
  mysql = require('mysql'),
  app = express(),
  port = 8080;

//app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
//app.use(bodyparser.urlencoded({ extended : true }));
//app.use(express.json({ limit: "200mb" }));

app.use(function(req,res, next) {
  res.locals.loginDB = mysql.createConnection({
    host: "localhost",
    user: "removed before posting",
    database: "removed before posting",
    password: "removed before posting"
  });
  res.locals.rta = rta;
  next();
});


//ImportRoutes
const loginRouter = require("./routes/login");
const registerRouter = require("./routes/register");

//Setup ImportRoutes
app.use("/AuthTest/login", loginRouter);
app.use("/AuthTest/register", registerRouter);


app.listen(port, () => {console.log("its alive!")});

I included commented out code I have tried. Now here is the problematic route up until the part it fails so ignore missing brackets after the return, the rest is irrelevant as its flow is cut off by the return statement.

const express = require("express");

//Setup express server routers
const router = express.Router();

//On get request
router.get('/', async (req, res) => {
  var test = req.body;
  res.status(200).send({
    success: false,
    message: "Test.",
    body: test,
    headers: req.headers
  });
  return;

Image of post result after making the route a post route

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

OMG THANK YOU @Phil, what he said about the 404 code meaning the post was still trying to do a get request made me suddenly remember a long while ago I made a folder named "login" in the apps folder in my websites publichtml folder. Meaning instead of calling express it was interpreting my request as a get, I believe because after removing the folder I was able to use the endpoint /login instead of /logins which worked and was the nail on the head to solving this!

over 4 years ago · Santiago Trujillo 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!