Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Postman render a file from a base64 string?

Tags:

postman

One of my server responses is returning a PDF as a base 64 encoded string.

E.g.

{
    "fileName": "my-file.pdf",
    "base64EncodedFileContent": "JVBERi0xLjcKJeLjz9MNCj...

Is it possible to render this 64 bit string as a PDF inside my Postman collection?

I've looked at decoding the string into an environment variable using CryptoJS.enc.Base64.parse

I've also looked at https://docs.postman-echo.com/ to perhaps send the bytes back to me with a content type of application/pdf, however it nests any body you send inside a JSON object.

Note: This is just a tool of convenience to prevent people using the collection from having to use an external base64 -> PDF converter

like image 788
Andy N Avatar asked Apr 03 '19 14:04

Andy N


People also ask

Can Postman decode base64?

Encode/Decode the variable/response using Postman itself To Base64 encode/decode, the quickest way is to use JavaScript methods btoa, atob: atob — It turns base64-encoded ASCII data back to binary. btoa — It turns binary data to base64-encoded ASCII.

How do I use base64 encode in Postman?

Encode "postman" to Base64 formatSimply enter your data then push the encode button. To encode binaries (like images, documents, etc.) use the file upload form a little further down on this page. Destination character set.

Can we convert base64 to JSON?

The Base64 to JSON online tool lets you convert the base64 string into JSON. Just load your base64 string and click on the convert button it will automatically decode into JSON data.


1 Answers

You shouldn't need to import CryptoJS to decode base64... you could use something like this;

const response = pm.response.json().base64EncodedFileContent;
var decodedMessage = atob(response);
console.log(decodedMessage);

This might also help with 'vizualising' PDF's in Postman https://community.postman.com/t/visualize-pdf-on-postman-v8-4-0/23856

like image 66
w4dd325 Avatar answered Sep 20 '22 12:09

w4dd325