50

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.

pkamb
  • 30,595
  • 22
  • 143
  • 179
Harsha W
  • 2,805
  • 3
  • 35
  • 62
  • 4
    Hi, you may have a look at this link: https://stackoverflow.com/questions/244777/can-comments-be-used-in-json?rq=1 – A.Joly Jul 10 '17 at 08:59
  • 1
    Does this answer your question? [Can comments be used in JSON?](https://stackoverflow.com/questions/244777/can-comments-be-used-in-json) – Henke Feb 17 '21 at 12:36
  • 2
    There is nothing magic about that body in Postman. It is just plain JSON. So unfortunately - as stated by the accepted answer to [Can comments be used in JSON?](https://stackoverflow.com/a/244858) - the answer is ***No***. A possible workaround is to add a "phony" field to your JSON data, like `"_comment": "comment text goes here...",`. - Of course, the trick to *comment out* some of your JSON data would be to simply change the name of the key that you want to "disable". – Henke Feb 17 '21 at 12:49

7 Answers7

25

You can write documentation and comments using the description section of the requests, collections or folders.

Pratik Mandrekar
  • 8,930
  • 3
  • 40
  • 64
9

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

pkamb
  • 30,595
  • 22
  • 143
  • 179
7

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.

Theo
  • 1,584
  • 1
  • 17
  • 39
  • 2
    This works great! But I needed to additionally update header: `pm.request.upsertHeader({key: 'Content-Type', value: 'application/json'});`. It seems like updating body resets content type to plain text. – ImSpeakEnglish Mar 26 '22 at 13:24
4

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.

pkamb
  • 30,595
  • 22
  • 143
  • 179
Subdex
  • 71
  • 7
2

It has been done by Script
https://community.postman.com/t/request-body-should-be-able-to-be-commented/8288

Pre-request Script created an object and convert it to a string then expose it

object = {
   // product: “{{displayName}}”,
   price : “15.5”
}
pm.environment.set(“object”, JSON.stringify(object));

Request Body called the variable object

{{object}}
Bunthai Deng
  • 769
  • 1
  • 8
  • 29
0

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

enter image description here

abadgujar
  • 38
  • 7
-1

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

pkamb
  • 30,595
  • 22
  • 143
  • 179
Praveen
  • 791
  • 6
  • 15