I'm trying to GET a file file (source) and .pipe to a destination Box Upload endpoint using NodeJS Request. This approach worked fine on other cloud storage, like Dropbox, but Box requires multipart/form-data upload, so it fails with bad request. Due some requirements, I cannot use the Box NPM package and would prefer this pipe approach.
I'm probably missing some timing here as there is an error output:
stream.js:74
throw er; // Unhandled stream error in pipe.
^
Error: write after end
at ClientRequest.OutgoingMessage.write (_http_outgoing.js:439:15)
at Request.write (/node_modules/request/request.js:1514:27)
at Request.ondata (stream.js:31:26)
at emitOne (events.js:96:13)
Tried use form-data, but no success, just changed the error and, aparently, the content-length was not right.
And here is my source code.
var source = {
url: SOURCE_DOWNLOAD_URL,
method: "GET",
headers: {
'Authorization': 'Bearer ' + SOURCE_TOKEN
},
encoding: null
};
var destination = {
url: 'https://upload.box.com/api/2.0/files/content',
method: 'POST',
headers: {
'Authorization': 'Bearer ' + BOX_TOKEN
},
formData: JSON.stringify({
attributes: {
name: 'somename.txt',
parent: {
id: 1234
}
}
}),
encoding: null
};
request(source).pipe(request(destination)
.on('response', function (resDestination) {
// expecting 201, but returns 400
console.log(destination.method + ' ' + destination.url + ': ' + resDestination.statusCode + ' > ' + resDestination.statusMessage);
}));