How to get an image's base64 data in postman

108 Views Asked by At

The question

How do I get base64 data from an online image within postman?

Context

I'm doing a migration within postman. One part of this is migrating embedded images within messages. The embedded images are inserted in the html like this:

<img src="https://attachment.freshdesk.com/inline/attachment?token=LongTokenHere" data-id="9198554508" alt="image.png" width="562" height="181">

Goal

Replace the image link with base64 data like this:

<img src="data:image/png;base64, iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" data-id="9198554508" alt="image.png" width="562" height="181"/>

Progress

I can download the image's data in which I assume is byte data, convert it to ascii (which I assume is the base64 format), and upload it. But when uploading it like that, it ends up as a broken/unviewable image.

pm.sendRequest(link, (err, res) => {
    err ? reject(err)
        : resolve({link, raw: res.text(), imageData: btoa(res.text())});
});

This does get me what seems to be base64 data: (a small snippet from the full file) /VBORw0KGgoAAAANSUhEUgAAAmcAAAD9CAYAAAD9bSdzAAAgAElEQVR4Af39f2xc/f39/f39/f1xV/1X/f26N0oUdSkrEP12/f0iAWkr/f39Bf0obTf9/Qv9/f1y/SH9OG40dXH9/TAQ/

Other things I tried

Within postman, I can get the link, and then the body preview shows the image as it's supposed to look. I can then save response to file, which shows a working png. When comparing both sources in notepad++, they do look alike for a bit:

Enter image description here

I also tried to see what happened when trying to convert the base64 data to a file using https://base64.guru/converter/decode/file, there I got this message: Your browser cannot display the file as “application/octet-stream”.. I'm unsure what to do with that information as the response headers show me content-type: image/png.

1

There are 1 best solutions below

3
Jurjen Waanders On

It is because the Content type of the server returns Content-Type: application/octet-stream. Which is not a valid image type. IE8 may try to show it anyway, which Chrome apparently won't.

Please refer to this post. https://stackoverflow.com/a/9046157/23185565