How to get the raw response body in Tests scripts?

I am trying in my test scripts to compute message digests on response bodies data to validate them.
I am using the pm.response.text() to compute the message digests.

That works as long as the body contains ASCII data.
But as soon as the body contains data other than ASCII, it does not work anymore as expected.

The problem occurs because the content returned by pm.response.text() is not the raw data, but is somehow modified.
I use the following code to verify the message digest and the content of pm.response.text() as an hexadecimal representation:
var rawBody = pm.response.text();
var hexBody = β€œβ€;
for (i = 0; i <rawBody.length; i ++) {
hexBody += rawBody.charCodeAt(i).toString(16);
}
console.log("MD5: " + CryptoJS.enc.Hex.stringify(CryptoJS.MD5(pm.response.text())));
console.log("Hexa data: " + hexBody);

For a received body with one β€œ0x61” byte (which is β€˜a’ in ASCII), I get the following displayed in the console:
β€œMD5: 0cc175b9c0f1b6a831c399e269772661”
β€œHex data: 61”

For a received body with one β€œ0x97” byte, I get the following displayed in the console, which is obviously wrong (MD5 should be c444b580079efb1fe408f17f029e5d35):
β€œMD5: 9b759040321a408a5c7768b4511287a6”
β€œHexa data: fffd”

How can I get the unmodified response raw body (or decoded if I received deflated, gziped, etc. content) ?

1 Like

Same problem when attempting to display an image in Visualize.

var template = `
<div style='background-color:white;'>
<div>Image:</div>
<image src='data:image/png;base64, {{imageBase64}}' />
</div>
`;

pm.visualizer.set(template,
    {
        imageBase64: btoa(pm.response.text())
    }
);

image

I had a similar question and got this response from Postman team. Posting it hear to help others. It’s not a great answer, but it might help depending on your situation.

As I understand it, you would like to parse a PDF file and then calculate MD5 value, correct?

According to my search, it seems that there is no method that can directly parse a PDF file. However I found a workaround that can be implemented in the backend shown in this YouTube channel: https://www.youtube.com/watch?v=zwfwJ165zHw

Once PDF-parsing is done, you can use CryptoJS library to calculate MD5 value as shown in this thread:
javascript - How to compute a md5 hash in a pre-request script in PostMan? - Stack Overflow

Hi @damiengarrido/@shresthasu,

To get raw binary data there is a way in postman by extracting stream of data from pm.response object. You can use below snippet to extract the data and specify the byte stream.

const mark1 = (pm.response);
console.log(mark1.stream.toString('utf-8'));

To convert the byte stream to base64
console.log(btoa(mark1.stream));

Hope it helps :slight_smile:

4 Likes