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

325
Views
How to post request?

why is the code outputting an empty req.body

const express = require("express");
const app = express();

app.use(express.urlencoded());

app.get("/", (req, res, next) => {
  res.send(`<script>
    fetch("/push", {
        body: "someText",
        headers: {
            "Content-Type": "text/html; charset=UTF-8",
        },
        method: "POST",
    }).then((data) => console.log(data));
</script>`);
});

app.post("/push", function (req, res, next) {
  console.log(req.body); //empty here //{}
  res.send(req.body);
});

app.listen(3000);
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Your fetch statement makes a POST request with Content-Type: text/html, which is highly unusual. express.urlencoded() does not fill req.body for this content type, it is only meant for Content-Type: application/x-www-form-urlencoded.

You can parse arbitrary textual content with code like this:

var body = "";
req.on('data', function(chunk) {
  body += chunk.toString();
})
.on('end', function() {
  res.send(body);
});
over 4 years ago · Santiago Trujillo Report

0

Just add the express.urlencoded({ extended: true }) also use the express.js

const express = require("express");
const app = express();
app.use(express.urlencoded({ extended: true }));
app.use(express.json())

app.get("/", (req, res, next) => {
  res.send(`<script>
    fetch("/push", {
      body: "someText",
      headers: {
        "Content-Type": "text/html; charset=UTF-8",
      },
      method: "POST",
    }).then((data) => console.log(data));
  </script>`);
});

app.post("/push", function (req, res, next) {
  console.log(req.body);
  res.send(req.body);
});

app.listen(3000);
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!