I am trying to upload a file in a https request using form data. I have this
function uploadDocument(filepath) {
var options = {
host : 'myserver',
port : 443,
path : '/platform-api/v1/documents',
method : 'POST',
headers : {
'Authorization' : 'Bearer 56356363',
'Accept' : 'application/json'
}
};
var req = https.request(options, function(res) {
var buffer = "";
res.on('data', function(chunk) {
buffer += chunk;
});
res.on('end', function(chunk) {
var json = JSON.parse(buffer.toString());
console.log(json);
});
});
var form = new FormData();
form.append('file', fs.createReadStream(filepath));
form.append('project_id', 4);
form.pipe(req);
req.on('error', function(e) {
console.log('problem with request:', e.message);
});
req.end();
}
but I get back
problem with request: write after end
{ errors:
{ file: 'missing-required-key',
project_id: 'missing-required-key' } }
Does anyone know what is wrong?
Thanks