Ignas Poška
Ignas Poška

Reputation: 77

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?

Upvotes: 4

Views: 28266

Answers (1)

Terry Lennox
Terry Lennox

Reputation: 30725

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;
}
..

Upvotes: 8

Related Questions