简体   繁体   中英

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?

As i said in my comment, you need to wait until you have the image to make the post request. If you wanted to pipe the streams, you could try something like this...

request.get(imageUri).pipe(request.post(uploadUri));

Hope that helps.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM