thunder-client-support icon indicating copy to clipboard operation
thunder-client-support copied to clipboard

Allow json with comment in request body

Open CaselIT opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe.

While developing it really useful to comment parts of a json request body, but currently there is no option that I could find to strip the comment before sending the request

Describe the solution you'd like

Ability to comment out part of a json request body

Describe alternatives you've considered

In postman I achieved something similar adding a script that filters the lines in the body starting with a comment before sending the request. I could not find any option to add a script to run before the request are sent so this solution is not currently possible

Implementation:

A simple implementation could support only single line comments // that are trivial to filter out, but since this is an extension of vscode it may be possible to leverage the parser implemented by vscode to support json with comments

CaselIT avatar May 05 '22 16:05 CaselIT

Thanks @CaselIT for the feedback, will add to roadmap

rangav avatar May 05 '22 19:05 rangav

I've tried using filters for this, and I can create the filtered body to send in the request, but there is no way that I could find of telling thunder client to use the updated body instead of the original one. Replacing the value of tcType.request.body.raw does not work

Is there a way I've not found to use filters to support this functionality?

CaselIT avatar Dec 02 '22 13:12 CaselIT

@CaselIT how you are replacing comments in the filter?

currently the request object is read-only, modifying it will have no effect.

rangav avatar Dec 02 '22 13:12 rangav

@CaselIT how you are replacing comments in the filter?

currently the request object is read-only, modifying it will have no effect.

that's indeed what I'm asking. Filter can access the body, but have no way (that I could find) of returning an updated body

CaselIT avatar Dec 02 '22 13:12 CaselIT

If you need to remove comments before sending request, we can do that in the extension, if that helps?

rangav avatar Dec 02 '22 13:12 rangav

that would be really helpful, since when experimenting with an api it's very useful to comment part of the request to experiment with various inputs

CaselIT avatar Dec 02 '22 13:12 CaselIT

Sure, What logic were you using to replace comments in filter?

rangav avatar Dec 02 '22 13:12 rangav

A very banal

       bodyStr = bodyStr.split('\n').filter(line => !line.trim().startsWith('//')).join('\n')

CaselIT avatar Dec 02 '22 13:12 CaselIT

But it's probable that vscode offers something better already builtin, since it does support jsonc (json with comments) natively

CaselIT avatar Dec 02 '22 13:12 CaselIT

Thanks will verify it.

I am not using vscode editor, it’s ace editor.

rangav avatar Dec 02 '22 14:12 rangav

This feature is now implemented and published to marketplace, please update to v2.1.0

See all features released https://github.com/rangav/thunder-client-support/releases/tag/v2.1.0

Please let me know your feedback.

rangav avatar Dec 05 '22 05:12 rangav

Limitations:

  • Format button will not work with comments
  • In Body tab it will still show error

When you run request, the extension will remove comments and send body

rangav avatar Dec 05 '22 06:12 rangav

I've tried it and it seems to work. The limitations are a bit of a pain, but better than nothing. Thanks for implementing this change!

CaselIT avatar Dec 13 '22 18:12 CaselIT

I will research for any better library, that can do format, remove comments and support bigInt, when I have am free.

rangav avatar Dec 13 '22 18:12 rangav

I found this library https://github.com/kaelzhang/node-comment-json, but does not support bigInt (large numbers)

rangav avatar Dec 13 '22 18:12 rangav