I have a String and I want to convert it to a stream to send it as if it were a file through multipart
the request
.
I want to do it in the air, without generating the file on disk.
I am doing it this way:
const stream = require("stream")
module.exports = function () {
let readStream = new stream.PassThrough()
readStream.end(new Buffer('<file data>'), () => {
const options = {
url: '<url>',
method: 'POST',
json: true,
formData: {
file: readStream
}
};
request(options, (error, res)=> {
if(error) {
console.log(JSON.stringify(error))
}
});
})
})
}
...and it returns the following error:
{
"code":"BadRequestError",
"message":"MultipartParser.end(): stream ended unexpectedly: state = PART_DATA"
}
I can think of 2 ways:
no stream
formData
ofrequest
we send thecustom_file
.with stream
Readable
create astream
delstring
.Example:
Well, what is needed is to change the MIME type in the header.