1

when I am making an API request and trying to store the file from the response, it's giving me an error

TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of PassThrough

const fetchResp = await fetch(
      `rest-api-url`,
      { method: "GET", headers: headers }
    );


fs.writeFile("sample.mp4", fetchResp.body, (err) => {
      console.log(err);
    });

1 Answer 1

2

https://developer.mozilla.org/en-US/docs/Web/API/Response

Response.body -> A ReadableStream of the body contents.

That's why you need to use writeable streams - simple using pipe to pipe it from read to write stream.

const writeStream = fs.createWriteStream('sample.mp4');

// pipe the read and write operations
// read input file and write data to the output file

fetchResp.body.pipe(writeStream);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.