skip to Main Content

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

BrianOutput of Postman Command

2

Answers


  1. You can’t save to disk directly from a PostMan script.
    I would recommend the following work flow:

    Before you work with Postman:

    1. Create a local web server (in any language but Node.js would be one choice) and a POST-route to which you can send the base64-code and the id as your request body.
    2. Let the server convert this to a ‘blob’ and save to disk with the id as file name.
    3. Start the web server (for example on port 3000).

    In PostMan:

    1. Create a collection with two requests
    2. Let the first request go to the URL/route you that returns the student id and base64-encoded image.
    3. Store each one (id and base64-encoded image data) in two separate environment variables in the your test-script.
    4. Create the second request to your local web server.
    5. In the request body send the two environment variables.

    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.

    Login or Signup to reply.
  2. 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:

    // Retrieve the base64 image from the response
    const base64Image = pm.response.body;
    
    // Convert the base64 image to binary format
    const binaryImage = atob(base64Image);
    
    // Create a Blob object from the binary image data
    const blob = new Blob([binaryImage], { type: "image/jpeg" });
    
    // Create a file name using the student ID
    const studentID = pm.variables.get("studentID");
    const fileName = studentID + ".jpg";
    
    // Save the image to disk
    const fileReader = new FileReader();
    fileReader.onload = function () {
      const imageDataUrl = fileReader.result;
      pm.sendRequest({
        url: "https://postman-echo.com/post",
        method: "POST",
        header: {
          "Content-Type": "application/json",
        },
        body: {
          mode: "raw",
          raw: JSON.stringify({
            image: imageDataUrl,
            fileName: fileName
          }),
        },
      }, function (err, res) {
        console.log(res);
      });
    };
    fileReader.readAsDataURL(blob);
    
    

    I detail the code:

    1. First, you retrieve the base64 image from the response using pm.response.body.

    2. Then, convert the base64 image to binary format using atob().

    3. Create a Blob object from the binary image data using the MIME type "image/jpeg".

    4. Create a filename using the student ID, which is retrieved using pm.variables.get("studentID").

    5. Then use a FileReader object to read the image data and convert it to a data URL using readAsDataURL(). 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!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search