What version of Postman are you currently on? Ideally, pm.request.body.raw should strip away JSON comments from the request body and post only the raw JSON.
My current setup for testing values fails as lines 1 and 13 are valid JSON, presumably. That’s my theory anyway. I’d have thought there would be a way have comments in the request, but not have then parsed.
The JSON.parse function will be expecting the first character to be an { or [ to denote an object or array.
If in doubt, just console log “pm.request.body.raw”. If its still got the comments in there, then the parse will fail.
However I have just tested this with the Postman Web UI, and pm.request.body.raw does strip comments if they exist from the body so checking your version number and console logging it directly would seem appropriate troubleshooting steps.
JSON comments can cause errors if included in the JSON gotten programmatically from the script tab, so the scripting library does the heavy lifting of stripping the comments off when accessed programmatically.
@chrisdoar a screenshot of what you see in the logs will be really helpful here as Mike recommended.
Thank you very much @gbadebo-bello and @michaelderekjones for your replies. Apologies for the delayed response. Actual work has got in the way of random playing around with API’s!
I’ve added a line to log the request body: console.log(pm.request.body.raw). This has, as expected, returned the comments within the request body
I get the parse fails as the comment following // isn’t valid JSON. My desktop version of Postman is 11.2.0. I’ll remove the comments then it will work for now but continue to look into a solution that will allow me to add comments.
Hey @chrisdoar. One thing I suspect here could be the formatting of the JSON itself. The comma seperators does look a bit off. But ideally, that should not be the case. I know we’ve had issues with JSON formatted as ascii in the past(not entirely sure)
@gbadebo-bello The pre-request script is not stripping the comment, but the post-response script with the same code is. Therefore the JSON.parse will work in the post-response script, but not in the pre-request script.
Looking at the body that was sent, you can see that the comments are stripped at that point.
Thank you for that @michaelderekjones. Looking at what you’ve done I’ve replicated it on my API call and can see the comments aren’t stripped from the body on the pre-request script but are on the post-request script
Thanks for catching this @michaelderekjones . That’s super interesting and I’ll forward this to the relevant team as it should be working on both pre-request and post-response scripts.