Kong / insomnia

The open-source, cross-platform API client for GraphQL, REST, WebSockets, SSE and gRPC. With Cloud, Local and Git storage.
https://insomnia.rest
Apache License 2.0
34.67k stars 1.96k forks source link

Add curly brace escaping feature #3119

Closed iinuwa closed 3 years ago

iinuwa commented 3 years ago

Double curly braces {{ ... }} are used to inject Insomnia environment variables, but if your payload has a double curly braces pattern, the only way to send this to the upstream is by disabling rendering of the request body. However, that means you can't use environment variables at all in this payload.

It would be nice to be able to escape Insomnia environment variables so you can use both patterns in one payload.

TheSamsa commented 3 years ago

Yes, this would be nice!

dimitropoulos commented 3 years ago

I'm interested in hearing more about this. Could you please provide a concrete example on one such request (say, that we could use to build a failing test that we would then make pass by implementing your feature).

TheSamsa commented 3 years ago

First: It seems I can do what I need if I enable "Skip rendering request body". Since my Insomnia Environment is only used for the URL, but not in the request body. But as soon as I want to extend my request chain I'll run into trouble (if I want to use responses from a request before)

I have a REST API where the user can POST some Template file, and our template engine uses {{ .var }} syntax to fill in values. The user does send something like this

{
  "name": "template name",
  "content": "Example {{ .title }}",
  "keys": [
    {
      "key": "title",
      "type": "string"
    }
  ]
}

But if I want to use it as request body in insomnia it does tell me I don't have a environment variable title specified. Which is true, but this is just my request, I do not want to paste a Insomnia Environment variable there.

dimitropoulos commented 3 years ago

thanks. what would be the syntax to tell insomnia you did have a title variable that you did wan to paste in there? how would you suggest we differentiate between the two?

TheSamsa commented 3 years ago

My fist impulse would be to escape the \{\{ .title \}\} if I want insomnia to know it is only text. Well it is kinda ugly, but I could live with that (regex anyone?). And it does match the default escape sequences out there.

On the other hand I understand this is some niche case ;)

iinuwa commented 3 years ago

Using backslashes was my first inclination when I figured out it didn't work. I think the only problem with the above is that it wouldn't be valid JSON (although using templates besides within JSON strings also results in invalid JSON. There are also other formats supported by Insomnia besides JSON, so that might not be a big deal.)

Some alternatives? E.g. {{{{ title }}}} (two braces escapes a single brace) or {{{ title }}} (surround template with one pair of curly braces to ignore templating) \{{ .title }} (prefix template with backslash to ignore templating)

I agree that it's a niche for payloads that include curly brace templates inside them (for me I was working with Teams webhooks that uses curly brace templates to enable formatting the date/time in the user's local timezone). If it were pursued, maybe it would be an advanced option that is disabled by default.

TheSamsa commented 3 years ago

Another alternative could be an escape sequence inside the curly braces:

{{ #.title }} (everything after # is printed raw

develohpanda commented 3 years ago

Hey folks, for the time being, is disabling rendering of the request body an option? This does mean that template tags and environment variables cannot be used, but it also means your curly brace payload would work.

This option is available under request settings for each individual request.

image

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

develohpanda commented 3 years ago

Actually it seems like raw tags should be able to cater for this!

inabajunmr commented 3 years ago

The following pattern I tested works well also.

{{ '{{variable}}' }}

ref. https://github.com/mozilla/nunjucks/issues/604#issuecomment-465493535

develohpanda commented 3 years ago

Thanks for that @inabajunmr! Just tested it myself and it seems to work as well, although Insomnia attempts to add a pill in the UI for {{ '{{variable}}' }} and spectacularly fails. The syntax for the raw tag would be {% raw %}{{variable}}{% endraw %} which is also standard Nunjucks syntax and has special case handling in Insomnia.

image

@iinuwa I will close this issue as resolved because it does seem to work without disabling rendering entirely - anything inside the raw tags will be used raw, and other areas in the same text field can still use environment variables.

Please re-open this issue if it does not cater for your use case!

tyler71 commented 1 year ago

For reference, this also worked well surrounding an entire JSON POST body

{% raw %}
[
  {
    "valuea": {
      "_gte": "{{ date_from }}"
    }
  },
  {
    "valuea": {
      "_lte": "{{ date_to }}"
    }
  }
]
{% endraw %}