简体   繁体   中英

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

{}

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM