9

I am using axios to request an image from a url like this:

const response = await axios.get('https://asite.dom/image/url', { responseType: 'arrayBuffer' }); 

I don't need and don't want to save the file locally. I just need the plain binary data for checking the image dimensions.

Thus I don't want to do this

const bufferImage = Buffer.from(response.data, 'binary');

because that's a buffer not the binary data.

I've tried to access

response.data

directly, but that's not the image's binary data either.

Can anybody help me with this?

1 Answer 1

9

Just tested this and it works:

const response = await axios.get(
  'https://example.com/image.png', { responseType: 'arraybuffer' }
); 

const bin = response.data.toString('binary');
console.log(bin);

The key seems to be that you capitalized the b in arraybuffer. Also, response.data is already a buffer, so you can just directly convert it to a string.

Sign up to request clarification or add additional context in comments.

2 Comments

Yep, that was the problem.
OMG. THANK YOU! I've spent a day and a half trying to find the right combination. I think a lot of places have the uppercase b in the documentation

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.