Pre-Request Script Need Actual Body for Hashmac calculation

Hey All,

I have googled and looked for this answer but been unable to find what I need

During the pre-request script I do a hashmac calculation which requires the body to actually match what postman sends.

This is normally fine until people start using enviroment variables and comments in their json body’s.

I can easily fix the json body for enviroment variables using

requestBody = pm.variables.replaceIn(requestBody);
hashedPayload = CryptoJS.enc.Base64.stringify(CryptoJS.MD5(CryptoJS.enc.Utf8.parse(requestBody)));

// I then set the Enviroment header for the hashmac
postman.setEnvironmentVariable('hmacAuthHeader', getAuthHeader(request['method'], requestUrl, request['data']));

However comments seem to be a bit more tricky.

I am wondering if anyone knows how to get the actual request body that gets sent in a post after it has had all the comments and enviroment variables replaced and removed during a pre-request script.

Thanks in advance.

Hi @jaie-chamsoft. Welcome to the Postman Community.

There currently isn’t any way to strip json request bodies of their comments. You will have to do this manually.

pm.request.body will return the JSON body and you can use an external CDN package such as strip-json-comments to strip comments from this JSON.

Please go through this article to learn how to use an external library in a Postman script.

Hey @gbadebo-bello

Thanks for the reply and advice!

I was looking at doing some thing like this and tried a library which stripped the comments out, but I experienced an issue where the way it was stripping out comments is different to how Postman was stripping out comments and once sent my hashmac was wrong.

I cant see strip-json-comments in the list of available libraries, Manage API data and workflows using Postman JavaScript objects | Postman Learning Center, that postman automatically supports, can you advise if this is what postman uses to strip the comments?

Is there no way to get the actual body that will be sent with all the comments and values replaced? Postman is doing this at some point in the process and I would love a function which replicates what is done so I can get an exact copy.


Hi @jaie-chamsoft.

strip-json-comments isn’t a built-in library. You will have to import it as an external library using this hack.

I can’t confirm that this is what Postman uses internally, but let me reach out to the team that manages this and get back to you. Currently, there isn’t a way to get the actual request body programmatically, but I have communicated this feedback with the team!

Additionally, you can make a feature request here. This will let you get notified when this feature has been worked on.


We currently use strip-json-comments internally. So working with that library should work.

Alternatively, here’s an approach that gives you more control over the request body since you’re working with HMAC, which can be quite sensitive to slight differences.

We have support for programmatically mutating the request body in your pre-request scripts.

The following pre-request script will remove comments from the raw mode body.

// Strip JSON Comments
if (pm.request.body.mode === 'raw') {
    const rawData = pm.request.body.toString();
    const strippedData = rawData.replace(/\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, (m, g) => g ? "" : m)

    pm.request.body.raw = strippedData;
    pm.request.headers.add({key: 'Content-Type', value: 'application/json'});
1 Like

Awesome, thank you!!
This works great.
I have added a new feature request here: Ability to get the request body in pre-request script of the postman transformed body · Issue #12606 · postmanlabs/postman-app-support · GitHub
maybe someone else could use this too.

Have a great day!!!

I’m glad you found it helpful. Thank you for raising the feature request!!

Can you nark that response as the solution so it’s easy to find in the future? Thanks!

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.