Insomnia: [Question] Stringify before sending

Created on 29 Mar 2019  Â·  4Comments  Â·  Source: Kong/insomnia

  • Insomnia Version: 6.3.2
  • Operating System: Win 10

Details

I need to send a json string containing a json inside to a rest endpoint.
Currently I write it like this:

"{` \"person\": { \"name\": \"Name of the person.\" } }"

but it's difficult to write it and hard to read. I'd like to write it somehow like:

return JSON.stringify(
  {
    person: {
      name: "Name of the person.\"
    }
  }
);

so I can clearly see the data to be sent and wouldn't have to escape characters.

I believe it's not possible now, so I'm marking this as a feature request.

Most helpful comment

One other thing that might be useful:

If you have an object as an environment variable like this:

{
  "myEnvVar": {
    "foo": "bar"
  }
}

you can stringify it using Insomnia's template language (Nunjucks) by applying a filter like this:

{{ myEnvVar | dump }}

https://mozilla.github.io/nunjucks/templating.html#dump

I'm going to close this issue as it seems like the question has been answered but feel free to follow up if it's still unclear.

All 4 comments

Is your use case to encode some json inside a json property?
Many APIs are "envelope" APIs, that is they have a JSON model and are used to carry a client-defined payload. Most clients would use JSON for the payload so we end up needing to write JSON as a string value for a JSON property.

I wrote a plugin for this use case: https://github.com/senoctar/insomnia-plugin-inception
Let me know if it works for you.

Woah, that plugin is very cool @senoctar!

One other thing that might be useful:

If you have an object as an environment variable like this:

{
  "myEnvVar": {
    "foo": "bar"
  }
}

you can stringify it using Insomnia's template language (Nunjucks) by applying a filter like this:

{{ myEnvVar | dump }}

https://mozilla.github.io/nunjucks/templating.html#dump

I'm going to close this issue as it seems like the question has been answered but feel free to follow up if it's still unclear.

It was non obvious for me (😴) but remember you can take any helper and use that in an environment variable. So I used Response -> Body Attribute in an environment variable and then put that where I wanted with dump so that the value from the response was re-stringified (escaped){{ myVar | dump }}.

Was this page helpful?
0 / 5 - 0 ratings