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

335
Views
app.use(express.raw()) not working in Express.js

I am making an Express application that takes in (binary) post data. Here is my code below.

Server-side:

var express = require('express');
var app = express();
var PORT = 3000;

app.use(express.raw());
    
app.post('/', function(req, res) {
    console.log(req.body);
    res.end();
});

app.get('/', function(req, res) {
    res.sendFile(__dirname + '/index.html');
});

app.listen(PORT, function() {
    console.log("Server listening on port", PORT);
});

index.html:

<script>
  fetch("/", {
    method: "POST",
    body: "hello world",
  });
</script>

However, when I run this code, when logging the request body, it logs an empty object. And when I looked at the documentation, it says that the request body will be an empty object if there was no body to parse. But I had a body in the request. What am I doing wrong here?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

For express.raw() to parse, add "Content-Type": "application/octet-stream" inside headers in your fetch(). Like so:

fetch("/", {
  method: "POST",
  body: "hello world",
  headers: {
    "Content-Type": "application/octet-stream",
  },
});
about 4 years ago · Juan Pablo Isaza Report

0

express.raw() only works if you set your Content-Type header in your fetch to `application/octet stream. So your new fetch method would look like this.

fetch("/", {
    method: "POST",
    body: "hello world",
    headers: {
      "Content-Type": "application/octet-stream"
    }
});

But what if you don't want the content type to be application/octet-stream? In this case, you can specify in the express.raw() options that the content type to parse the body can be any type. How to do this? Replace this:

app.use(express.raw());

With this:

app.use(express.raw({type: '*/*'}));

The type option is the needed content type for body-parser to parse the body, and the */* means that any content type will be accepted.

about 4 years ago · Juan Pablo Isaza Report

0

Use this for your usecase app.use(express.text());

about 4 years ago · Juan Pablo Isaza 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!