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.
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.
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.
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.