How to place comments in Postman?

Viewed 80564

How to place comments inside Postman? Specifically in the JSON request body section?

I want to comment-out a particular key or value from the request body so that it is not sent.

Commenting out a JSON key/value pair with // or /* ... */ appears as a styled comment inside Postman:

comments appear to work

But sending this request results in server errors such as the below, and it's clear that the commented-out line is being sent as part of the request body:

Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser) at [Source: (org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream); line: 2, column: 6 ]

The JSON spec does not allow comments: Can comments be used in JSON?

I want Postman to strip the commented lines prior to being sent in the request.

7 Answers

A "Comments" option/button is above the send button for every request.

But still, we can't add a comment in the request body, maybe in future, they'll provide this feature.

screenshot

Finally, since Postman v8.3.0 you can do this in your collections pre-request script:

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

This strips all comments from the json and sets the current body to the cleaned one, there are more examples for other body types (GraphQL, URL Encoded, Form Data) in the original github post this code is based from.

I checked with GitHub where Postman has their feature requests and bugs tracked, Link here:

Feature request: Raw body editor JSON comment #3358
https://github.com/postmanlabs/postman-app-support/issues/3358

You can go and add comments there so that this feature would be considered for their next release. Also, I found out that you can copy and paste <!-- comment--> to make a comment.

Not the actual question's answer, but this addresses the highly upvoted comment below the question:

I just want to disable some code not want to add comments or descriptions..how can I do that? – Mahender Reddy Yasa

Ignoring the comment validation and sending request is now working for me in latest postman 7.7.3 64 bit version of windows.

Postman inputs

In Postman v9.13.0 you can do so using block comments

enter image description here

Related