I am using npm's request package to do a post of a file-buffer to a REST api written using meteor.js restivus package. My node.js client code that posts to the api is as follows:
url = 'http://localhost:3000/api/v1/images/';
fs.readFile('./Statement.odt', function read(err, data) {
if (err) {
throw err;
}
console.log(data); //At this stage the file is still a buffer - which is correct
var file = data;
request.post({
uri: url,
headers: {
'X-User-Id': userId,
'X-Auth-Token': authToken
},
form: {
file: file, //Inside the request.post the file is converted to binary encoding
name:"Statement.odt"
}
}, function(err, httpResponse, body) {
if (err) {
return console.error('post failed:', err);
}
console.log('Get successful! Server responded with:', body);
});
});
The problem/issue here is that inside the request.post the file is converted to binary-encoded blob. See my comments in the "form:" property of the first argument of "request.post" in the code above. This becomes a problem on my meteor.js server where the file is required as a buffer instead of a binary encoded file. (For info: I am using Ostr-io/files' GridFS to store the file - it requires the file to be a buffer)
If there is no way other than to pass the file as an encoded string, then is there a way to convert that encoded blob back to a buffer server-side where I am using/talking meteor.js? Please help!
If you need more info, please let me know, I'll provide.