I'm using an API to upload a CSV file. I create the CSV file in memory from a String and upload it using the request
module. However, I'm having trouble creating the Readable Stream from the String. I followed a SO answer on How to create streams from string in Node.Js. Here is my code for that solution:
var importResponse = function(csv, callback){
stringify(csv, function(err, output){
const s = new Readable();
s._read = () => {};
s.push(output);
s.push(null);
request.post({
headers: {'X-API-TOKEN':token, 'content-type' : 'multipart/form-data'},
url: 'https://ca1.qualtrics.com/API/v3/responseimports',
formData: {
surveyId: 'SV_123',
file: {
value: s,
options: {
contentType: 'text/csv; charset=utf-8'
}
}
}
}, function(err, res, body){
if(err || res.statusCode !== 200){
console.log(err || "Error status code: " + res.statusCode);
console.log(body);
return;
}
});
});
}
The csv
variable looks like [["QID1","QID2"],["1","2"]]
and the output from stringify looks like "QID1,QID2\n,1,2\n"
.
This solution gives me the error Unexpected end of input
{"meta":{"httpStatus":"400 - Bad Request","error":{"errorMessage":"Unexpected end of input"}}}
If instead I use memfs
, it works fine
const fs = require('memfs');
var importResponse = function(csv, callback){
stringify(csv, function(err, output){
// Create file in memory
fs.writeFileSync('/data.csv', output);
request.post({
headers: {'X-API-TOKEN':token, 'content-type' : 'multipart/form-data'},
url: 'https://ca1.qualtrics.com/API/v3/responseimports',
formData: {
surveyId: 'SV_123',
file: {
value: fs.createReadStream('/data.csv'),
options: {
contentType: 'text/csv; charset=utf-8'
}
}
}
}, function(err, res, body){
if(err || res.statusCode !== 200){
console.log(err || "Error status code: " + res.statusCode);
console.log(body);
return;
}
});
});
}
How can I convert the output from stringify
to a Stream that I can use to upload via the api?