65

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
  • 33,281
  • 23
  • 160
  • 191
Harsha W
  • 3,162
  • 5
  • 43
  • 77
  • 5
    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

8 Answers8

25

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

Pratik Mandrekar
  • 9,362
  • 4
  • 45
  • 65
  • 34
    I just want to disable some code not want to add comments or descriptions..how can I do that? – Mahender Reddy Yasa Jul 23 '18 at 13:25
  • 3
    same question as @MahenderReddyYasa – Yaser Khahani Sep 22 '18 at 11:50
  • 2
    You can disable code using either a `// ` or a `/** – Pratik Mandrekar Dec 20 '18 at 15:26
  • 6
    OP is asking how to stop Postman including commented code in requests, this is the problem. – Marius Mar 05 '21 at 09:39
  • 1
    @PratikMandrekar not sure why this answer is accepted by OP but even I'm looking for an answer where I can comment a line of code in my json body without removing the lines, I tried `//`, `/**/` but these returned me error saying `"JSON parse error: Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('/' (code 47)): ` – Suresh Jul 14 '22 at 03:22
  • `maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser)\n at [Source: (PushbackInputStream); line: 10, column: 6]"` – Suresh Jul 14 '22 at 03:23
22

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
  • 2,262
  • 3
  • 23
  • 49
  • 8
    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. – mindaugasw Mar 26 '22 at 13:24
12

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
  • 33,281
  • 23
  • 160
  • 191
5

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

Pre-request Script create an object, 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}}
Ricardo
  • 3,696
  • 5
  • 36
  • 50
Spring
  • 831
  • 1
  • 12
  • 42
  • 2
    See it in action on YoouTube: [Add comments to JSON body in Postman](https://www.youtube.com/watch?v=bX4-cLPcD2Q) – Ricardo Jul 07 '22 at 20:42
  • That's the only solution that worked for me on the latest Postman for macOS (v9.24.0; Desktop Platform v9.22.2) – Ricardo Jul 07 '22 at 20:44
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
  • 33,281
  • 23
  • 160
  • 191
Subdex
  • 71
  • 7
1

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

enter image description here

abadgujar
  • 48
  • 8
  • 1
    Multi-line comment doesn't work in postman. I tried the same you showed above but it didn't work and sent /* *? block in the native request. – Rizwan Sep 13 '22 at 07:24
0

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
  • 33,281
  • 23
  • 160
  • 191
Praveen
  • 831
  • 7
  • 15
0

What you can do, you can add comments after your request object.

{
  "name":"Abc",
  "number":"278389239"
}

// Your Comment

/**

    Your Comments

**/

You can get a more clear image from this screenshot

Usama Majid
  • 1,103
  • 13
  • 13