question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to upload a file to third-party API (imgur.com) using Multer 2.0?

See original GitHub issue

Good 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:closed
  • Created 3 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jonchurchcommented, May 21, 2020

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.

1reaction
jonchurchcommented, May 21, 2020

So the form should be the entire request body, I got this working doing the following:

    const imgurForm = new FormData();
    imgurForm.append('image', req.file.stream);

      const result = await axios.post(
      'https://api.imgur.com/3/upload',
      imgurForm,
      {
        headers: {
          Authorization: `Client-ID ${CLIENT_ID}`,
          ...imgurForm.getHeaders(),
        },
      }
    );

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:

imgurForm.append('image', req.file.stream, 'myFile.jpg');

// alternatively, get it from multer

imgurForm.append('image', req.file.stream, req.file.originalName)

And if you need to pass any other fields to imgur, set them on the form:

imgurForm.append('description', 'here is my image description text')
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to upload files in NodeJS using Multer 2.0! - picnature
req . Start up your server using · npm start and let's console.log("File:", req.file) to make a request from our frontend. Change your...
Read more >
Multer: Easily upload files with Node.js and Express
For our demo, we'll build our backend using Node.js and Express. We'll set up a simple API in upload_files and start our server...
Read more >
multer imgur的推薦與評價,STACKOVERFLOW ... - 最新趨勢觀測站
In this project we will look at file/image uploading in Node.js with the Multer module. ... Your browser can't play this video. ......
Read more >
How to upload a single Image using Multer when req. file is ...
I had to clear the headers, so I can obtain the req.file ,. with this change it worked.
Read more >
How to send a File from React to NodeJS with Multer 2.0 ...
Multer 2.0.0 will be out soon! Learn how to send a file from your react frontend and parse it using the new Stream-based...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found