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

420
Views
application/octet-stream Content Type get null request body

There is a problem in application/octet-stream in sending request to node.js server. Request body is empty {} in server.

Question

Why the server take empty request body? and why it lost ?

Fetch Request

async function postData(url = '', data) {
                        console.log('data', data);
                        const response = await fetch(url, {
                            method: 'POST',
                            mode: 'cors',
                            cache: 'no-cache',
                            credentials: 'same-origin',
                            headers: {
                                'Content-Type': 'application/octet-stream',
                                'Content-Disposition': 'attachment'
                            },
                            redirect: 'follow',
                            referrerPolicy: 'no-referrer',
                            body: data
                        });
                        return response; //.arrayBuffer();
                    }

                    postData('http://localhost:3600/buffer', arrU8)
                        .then(data => {
                            console.log(data);
                        });

arrU8 is an Uint8Array by length : 33411528

Node.js post api

Express.js is used as standard library. This api is very simple and I want just return back the request body as response without any processing on body.

exports.buffer = (req, res) => {
    console.log(req);
    res.status(201).send(req.body);
};

Response

{}
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You've clarified that you're using Express.js and aren't using any middleware.

You have to use middleware to get req.body populated in Express.js. From the documentation:

req.body

Contains key-value pairs of data submitted in the request body. By default, it is undefined, and is populated when you use body-parsing middleware such as express.json() or express.urlencoded().

(my emphasis)

So your code is basically doing .send(undefined).

If you don't want to use middleware for some reason, the Express.js Request is an augmented version of Node.js's built-in IncomingMessage, so you can use those methods to read the data as (say) a Buffer or similar if you want.

about 4 years ago · Juan Pablo Isaza Report

0

After inspecting the body-parser module, you find that there are these scripts:

json.js
raw.js
text.js
urlencoded.js

They likely each match up to specific MIME-types. For application/octet-stream, you'd need to make use of raw.js. To do so, when initializing your express app, you should write:

app.use(require("body-parser").raw());

This will initialize the middleware that sets that req.body attribute, when the content-type is "application/octet-stream". Since req.body does not exist initially in the req object, it needs to be set by something. That something tends to be some function(s) in the middleware, or if you know, you could do it yourself (that implies looking at the request headers and parsing the stream).

To get direct access to the bytes stream of the request (and by inspecting the module's source code), you find that there's a stream being read from:

let stream = zlib.createInflate();
req.pipe(stream);

or

let stream = zlib.createGunzip();
req.pipe(stream);

I must say that I myself do not understand the details of the stream or the req object further than this.

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!