Brian Harry
Apr 30, 2023, 17:40 UTC
I have a get connection that with retrieve the photo of a student and it has the student id, though the image retrieved is on base64, can I export this automatically to jpg as part of my initial postman get script,so I request the photos and export and save them to jpg Thanks
I am completely new to Postman and Javascript.
Kind regards
Brian
I have created a GET Command that get the student photo, it comes back with the image in base64. I also have the student ID number, so I need to be able to save the base64 code to jpg with the student ID, ie 123456.jpg
Can you please advise upon how I can do this, as I need to get this resolved asap.
Kind regards
2
Answers
You can’t save to disk directly from a PostMan script.
I would recommend the following work flow:
Before you work with Postman:
In PostMan:
I don’t know if you are familiar with writing backend-code/web-servers but this is the best solution I can come up with.
If you don’t know how environment variables, scripting or collections work in Postman – look it up Postman’s documentation which is very well written.
I am not sure whether I fully understand what you mean, although I’ll try my best.
[In JavaScript] to convert the base64 image to a JPG and save it with the student ID as the filename, you can add the following code to your Postman pre-request script:
I detail the code:
First, you retrieve the base64 image from the response using
pm.response.body
.Then, convert the base64 image to binary format using
atob()
.Create a Blob object from the binary image data using the MIME type "image/jpeg".
Create a filename using the student ID, which is retrieved using
pm.variables.get("studentID")
.Then use a
FileReader
object to read the image data and convert it to a data URL usingreadAsDataURL()
. Once the data URL is generated, we send a POST request to Postman Echo with the image data and filename as JSON in the request body.Welcome to the community! Please, next time remember that your code should be directly written or pasted in Markdown, as above.
I hope it helps!