Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make an HTTP request with an application/octet-stream content type - node js

At the moment I am using npm module request to upload file with application/octet-stream content type. The problem is that I cannot get response body back. It is happening due to a known bug: https://github.com/request/request/issues/3108

Can you provide me an alternate ways to upload file to an API with an application/octet-stream content type?

like image 984
Ignas Poška Avatar asked Sep 20 '25 01:09

Ignas Poška


1 Answers

Have you tried loading the file to a buffer rather than a stream? I appreciate in many contexts a stream is preferable, but often just loading into memory is acceptable. I've used this approach with no issues:

const imageBuffer = fs.readFileSync(fileName); // Set filename here..

const options = {
    uri: url, /* Set url here. */
    body: imageBuffer,
    headers: {
        'Content-Type': 'application/octet-stream'
    }
};


request.post(options, (error, response, body) => {
if (error) {
    console.log('Error: ', error);
    return;
}

To do the same thing using a stream:

const options = {
    uri: url, /* Set url here. */
    body: fs.createReadStream(fileName),
    headers: {
        'Content-Type': 'application/octet-stream'
    }
};

request.post(options, (error, response, body) => {
if (error) {
    console.log('Error: ', error);
    return;
}
..
like image 115
Terry Lennox Avatar answered Sep 22 '25 01:09

Terry Lennox