Reputation: 3215
I have a client side form using XMLHTTPResponse that allows me to get response data saved as a file. It sets the response type as arraybuffer as follows before the rest of the blob conversion occurs:
xhr.responseType = "arraybuffer";
I have been searching and finding multiple methods to create an arraybuffer, but none that detail how to take the response in node to an arraybuffer. I am using unirest as follows:
unirest.post('http://myvendorsapi/Upload_PDF')
.headers({ 'Content-Type': 'multipart/form-data' })
.field('filename', filename)// Form field
.attach('file', fileloc)// Attachment
.end(function (response) {
console.log(response);
var returnfile = response.body;
// Need logic to convert to arraybuffer
});
How do I set the response type to an arraybuffer or convert my response to an arraybuffer?
Upvotes: 1
Views: 1324
Reputation: 574
If you want to get more raw response data, I would ditch the unirest library and use something thinner such as request. Unirest claims to 'parse responses' for you, which sounds like something that you don't want. If you simply want to save the raw response body to a file, using request, you could do something like this:
var formData = {
filename: filename,
file: fs.createReadStream(fileloc)
}
var req = request.post({url: 'http://myvendorsapi/Upload_PDF', formData: formData})
req.pipe(fs.createWriteStream('OUTPUT FILE NAME'))
Upvotes: 1