How to upload a file to third-party API (imgur.com) using Multer 2.0?
See original GitHub issueGood day to everyone. I’m using Multer 2.0.0-rc.2. I created a variable that stores the stream of my file that I’m sending from Postman to my API:
const getStream = require("get-stream"); const multerStream = await getStream(req.file.stream);
I logged it, and the stream is definitely there. Wonderful. But then I have to upload this file to imgur API. How should I do it? The post request below works if I put a direct link to any image file on the internet instead of WHAT GOES HERE?. But how to upload the file from my stream? I want to use streams since it’s better than temporary storing the file on disk or entirely in the memory. Thank you in advance, hope somebody could help me 😃
const imgurResponse = await axios.post( https://api.imgur.com/3/upload, { image: WHAT GOES HERE? }, config );
Issue Analytics
- State:
- Created 3 years ago
- Comments:6 (4 by maintainers)
Multipart stuff can be confusing, which is why multer is so useful for receiving these uploads! Sending them from node is it’s own special kind of pain. If you run into more issues, I’d suggest searching for tutorials about sending multipart forms w/ Node.js using axios or whatever your request library of choice is.
So the form should be the entire request body, I got this working doing the following:
I’m passing the entire
FormData
as the body. Any fields you need to send to imgur should be set on the form. Finally, I’m pulling the headers from the form using the.getHeaders()
method of form data.If you want to set the name of the file when using formdata and streams, pass it as the third argument:
And if you need to pass any other fields to imgur, set them on the form: