3

I dont want to send comment as part of JSON, I just want to have a comment as a reminder for my self in PostMan Body tab for JSON request.

For example, I have JSON body like:

/* just some comment here */
{
    "username": "mike",
    "usertype": 1,
    "userid": "3333333",
    "id": "kasd331"
}

If I remove the comment at the top (/* just some comment here */) and send this request and body, it will work. However, with the comment above, PostMan shows me error:

IOExceptionMapper:Unexpected character ('/' (code 47)): maybe a (non-standard) comment? (not recognized as one since Feature 'ALLOW_COMMENTS' not enabled for parser) at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@3b0fb52a; line: 1, column: 2 ]

How can I use comemnts in JSON body in PostMan?

pixel
  • 9,653
  • 16
  • 82
  • 149

3 Answers3

3

If you need it for one single request and not for the whole collection you can set the pre-request script like this:

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)));

Please refer to this answer for collection-level setting: https://stackoverflow.com/a/67493035/8247069

Theo
  • 2,262
  • 3
  • 23
  • 49
  • 2
    Been using the above piece of code for a long time. Thanks, Theo. Just realized a small idiosyncrasy of the same. By doing so in Postman's pre-request script, Postman effectively rewrites the auto-generated Content-Type header to 'text/plain'. If your backend APIs require 'application/json' mandatorily like mine, they will start failing. A workaround to this is to add Content-Type header manually in all your requests. Postman does not overwrite that or you can add this piece of code in the pre-request script as well- `pm.request.addHeader('Content-Type:application/json'); }` – Mukul Bansal May 20 '22 at 14:09
1

Adding comments as // is a javascript way , in postman javascript is supported only in test and pre request script sections .

Comments are not supported in body , you can add comments on the the request description or by clicking comment near to the send button.

enter image description here

or

enter image description here But cannot add comments specific to body

if you click console and check the request body:

enter image description here

you can see that what ever you give in body part is send as request body. So if your API is designed to ignore the comment and take only valid json then comment might work , else it won't

PDHide
  • 18,113
  • 2
  • 31
  • 46
0

Comments will work if the calling API support those. To comment any line in request body we can just use //. refer here

DYD
  • 1
  • 1