Search code examples
node.jsmultipartform-dataform-datanode-request

How to POST image as form data using npm request?


I have a specific task: I need to download an image from the source URL and upload it to another host via POST request as multipart/form-data. I'm trying to use node.js request library but never succeed. The following code doesn't send anything in the request body.

request.post({
  url: uploadUrl, 
  formData: {
    photo: request(imageUri)
  }
}, function (err) {
  if (err) console.error(err.stack)
});

I have tried posting directly through the form-data library, but it doesn't seem to work neither. How do I solve this without the creation of temp files?


Solution

  • The problem turned out to be that my imageUri had query parameters in it. I think this is a bug in form-data library. Removing query parameters solved the problem.