0

I am working on downloading image from url using nodejs. But I am unable to convert response to buffer. Image I receive from response is in gibberish form.. Following is my code:

app.get('/test', async (req, res) => {
  const data = await fetch("https://api.image/test.jpg");
  res.send(Buffer.from(data));
});

Response of fetch API is: enter image description here

Error after downloading image is:

Image is

I also tried following buffer encoding but I still getting above error after downloading image:

 Buffer.from(data,'base64');
 Buffer.from(data,'ascii');
 Buffer.from(data,'base64url');
 Buffer.from(data,'binary');
 Buffer.from(data,'hex');
 Buffer.from(data,'latin1');
 Buffer.from(data,'ucs-2');
 Buffer.from(data,'ucs2');
 Buffer.from(data,'utf-8');
 Buffer.from(data,'utf16le');
 Buffer.from(data,'utf8');

1 Answer 1

3

I think this should be like:

app.get('/test', async (req, res) => {
  const data = await fetch("https://api.image/test.jpg");
  res.send(Buffer.from(await data.arrayBuffer()));
});
Sign up to request clarification or add additional context in comments.

6 Comments

Tried, Not Working
It works for me, what's the error message, and which framework do you use, express?
I am getting error: "Error : TypeError: data.arrayBuffer is not a function"
It is working... Thank you very much
I am curious about why you were getting the "data.arrayBuffer is not a function" error before (Also it may help others)
|

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.