0

What would be the HTTP equivalent of this cURL request? I've tried to translate and haven't been able to pass the file correctly to the API.

$ curl -F "file=@test.txt" https://xxxxxx

I've tried:

const formData = {
  file: 'pathToFile'
}

request
  .post({
    url: 'xxxxxx',
    form: formData
  })
  .on('response', (response) => {
    console.log(response);
  });

which gives me a 5xx server error ~ file param undefined.

how would I go about doing this in Node.js? I'm using Request

uma
  • 99
  • 5

1 Answers1

0

Doc says to use fs.createReadStream(...) to pass a file:

var formData = {
  my_file: fs.createReadStream(__dirname + '/test.txt'),
};
request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) {
  if (err) {
    return console.error('upload failed:', err);
  }
  console.log('Upload successful!  Server responded with:', body);
});

Also the option is formData, not form. Unless we are speaking of different versions.

Alternatively, there is also node-fetch.

Hugues M.
  • 19,846
  • 6
  • 37
  • 65