Last active
March 21, 2022 20:56
-
-
Save kishannareshpal/f627efc2f21c41169a6ba1e32ab3f877 to your computer and use it in GitHub Desktop.
Strip comments from Postman requests with Raw JSON body.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * What is this? | |
| * - If you try to comment stuff when requesting using a Raw JSON body, | |
| * you'll know that it will most likely fail, as the request is sent with your | |
| * comments bit included. | |
| * This script is supposed to intercept your request body and strip every comment | |
| * it has and then apply the sanitized body to the request, so you can comment | |
| * all you want inside Postman JSON request body. | |
| * | |
| * How to use it? | |
| * - Copy and paste this snippet into Postman's Pre-request Script | |
| */ | |
| const body = pm.request.body; | |
| if (body.mode !== "raw" || !body.raw) { | |
| return; | |
| } | |
| const cleanBody = stripJSComments(body.raw); | |
| pm.request.body.raw = cleanBody; | |
| // ---------------------------------------------------------------------------------------- | |
| // Helpers | |
| // ---------------------------------------------------------------------------------------- | |
| /** | |
| * Removes any JavaScript comments from the {@link serializedBody} text. | |
| * | |
| * @param {string} serializedBody - Request body JSON as string | |
| */ | |
| function stripJSComments(serializedBody) { | |
| const literalValues = []; | |
| // Matches strings and temporarily replaces them with 'n###' so that | |
| // the next replace methods that are responsible to match comments, doesn't | |
| // match any literal value. | |
| // - These values are replaced back at the end. | |
| // - Notice that we use a number before the hashes. This is so that we can later | |
| // track down the value that was in that place. | |
| let cleanSerializedBody = serializedBody.replace(/(['"`]).*?\1/gm, function(match) { | |
| const newLength = literalValues.push(match); | |
| const index = newLength - 1; | |
| return `${index}###`; | |
| }); | |
| // Removes comments starting with // | |
| cleanSerializedBody = cleanSerializedBody.replace(/\/\/.*/gm, ''); | |
| // Removes cooments starting with /* */ | |
| cleanSerializedBody = cleanSerializedBody.replace(/\/\*[\s\S]*\*\//gm, '') | |
| // Removes comments starting with <!-- --> | |
| cleanSerializedBody = cleanSerializedBody.replace(/<!--[\s\S]*-->/gm, ''); | |
| // Insert our literal values back | |
| cleanSerializedBody = cleanSerializedBody.replace(/\d+###/gm, function(match){ | |
| const index = parseInt(match); | |
| if (index === NaN) { | |
| return ''; | |
| } | |
| return literalValues[index]; | |
| }); | |
| // Return the clean, non-commented version of the body | |
| return cleanSerializedBody; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment