Home > Software design >  axios response.data.pipe not a function
axios response.data.pipe not a function

Time:05-10

I'm using axios to download image in nodejs. I've added the following the code

const axios = require("axios").default;
const fs = require("fs");

const url =
  "https://images.pexels.com/photos/11431628/pexels-photo-11431628.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260";

async function downloadImage() {
  try {
    const { data } = await axios.get(url);
    data.pipe(fs.createWriteStream("sample.jpg"));
  } catch (error) {
    console.log(error);
  }
}

downloadImage();

But I get the following error

TypeError: data.pipe is not a function

CodePudding user response:

.pipe() can only be used on streams. To get axios response in streams, add a config object with responseType:'stream'

    const { data } = await axios.get(url, { responseType: "stream" });
    data.pipe(fs.createWriteStream("sample.jpg"));

  • Related