Getting binary content in Node.js using request
OK, after a lot of digging, I found out that requestSettings
should have:
encoding: null
And then body
will be of type Buffer
, instead of the default, which is string.
Getting binary content in node.js with http.request
The accepted answer did not work for me (i.e., setting encoding to binary), even the user who asked the question mentioned it did not work.
Here's what worked for me, taken from: http://chad.pantherdev.com/node-js-binary-http-streams/
http.get(url.parse('http://myserver.com:9999/package'), function(res) {
var data = [];
res.on('data', function(chunk) {
data.push(chunk);
}).on('end', function() {
//at this point data is an array of Buffers
//so Buffer.concat() can make us a new Buffer
//of all of them together
var buffer = Buffer.concat(data);
console.log(buffer.toString('base64'));
});
});
Edit: Update answer following a suggestion by Semicolon
How to POST binary data in request using request library?
The option in request library to send binary data as such is encoding: null
. The default value of encoding is string
so contents by default get converted to utf-8
.
So the correct way to send binary data in the above example would be:
const makeWitSpeechRequest = (audioBinary) => {
request({
url: 'https://api.wit.ai/speech?v=20160526',
method: 'POST',
body: audioBinary,
encoding: null
}, (error, response, body) => {
if (error) {
console.log('Error sending message: ', error)
} else {
console.log('Response: ', response.body)
}
})
}
Read binary data from request body of an HTTP trigger
The GitHub issue Azure Functions (node) doesn't process binary data properly describes the problem and suggests to set the Content-Type
header to application/octet-stream
.
curl -v \
-X POST \
-H 'Content-Type: application/octet-stream'
--data-binary @resources/example.pdf \
$function_url
# > Content-Length: 2505058
The Azure Function reads the request body with Buffer.from(req.body, 'binary')
.
const dataFromReq = new Uint8Array(Buffer.from(req.body, 'binary'));
context.log(`Read PDF file from request body with length ${dataFromReq.length}`);
// Read PDF file from request body with length 2505058
NodeJs send Binary Data with Request
Got it working , We need to create a stream of file using
fs.createReadStream(filename);
fs module.
request({
url: 'http://localhost/wordpress/wp-json/wp/v2/media/',
method: 'POST',
headers: {
'cache-control': 'no-cache',
'content-disposition': 'attachment; filename=' + filename,
'content-type' : 'image/jpg',
'authorization' : 'Basic token'
},
encoding: null,
body: fs.createReadStream('C://Users/as/Desktop/App _Ideas/hh.jpg')
}, (error, response, body) => {
if (error) {
res.json({name : error});
} else {
res.json(JSON.parse(response.body.toString()));
}
});
Related Topics
Undefined Values in Array(Len) Initializer
How Is () => {...} Different from () =>
Executing JavaScript from Python
In JavaScript/Jquery What Does (E) Mean
How to Access 'Window' (Target Page) Objects When @Grant Values Are Set
Is It Safe to Assume Strict Comparison in a JavaScript Switch Statement
Maximum Call Stack Size Exceeded on Npm Install
Trigger an Event with Prototype
How to Close a Dropdown on Click Outside
How to Understand Usecapture Parameter in Addeventlistener
Date.Setmonth' Causes the Month to Be Set Too High If 'Date' Is at the End of the Month
How to Calculate the Latlng of a Point a Certain Distance Away from Another
Where to Save a Jwt in a Browser-Based Application and How to Use It
Es6 Promise.All() Error Handle - Is .Settle() Needed
Where to Save a Jwt in a Browser-Based Application and How to Use It