Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

422
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda