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.81k stars 1.97k forks source link

[Feature Request] Add templates or default values for variables #1794

Open sturose opened 4 years ago

sturose commented 4 years ago

Details

This is perhaps best explained via some workflow examples.

Say I have a set of api calls to make in a particular order. These calls can be authenticated, or not. Here in lies my problem

If I write the requests so that they take the authentication token from a Login response...then those requests do not work for unauthenticated flows. I would need to duplicate all of those apis. What I really want is to be able to set in the template something like: if this value isn't here, use this default value (in this example the default value would be nothing, for unauthenticated).

This then brings me to another potential problem. Once I've tested an authenticated flow, I have no way to "log out", or clear the value.

What are the suggested ways around this problem?

From my limited view, the easiest way would be to two things 1) Have the ability to specify a default value if no value found via the filtering. 2) Have a button to click to clear out populated values so to be able to start with a clean slate. The Clear History mechanism does cover this, although it requires you to click back the previous request you made, clear it's history, then go back to the request you are intending to make...

gschier commented 4 years ago

Can you please specify which Template Tags you are using, as well as some examples of what your requests look like?

sturose commented 4 years ago

So, I have a template that is a tag from the Login Response It simply references $.auth_token

Except sometimes I want to use the same request for a scenario where the user is not logged in. I need to capture the value from the Login Response, and I'd like to do so automatically. But I also need a way to clear out that value without introducing a templating problem (trying to reference a value that isn't there)

sturose commented 4 years ago

What I really want is a way to set a global/environment variable from a received response, but also the ability to quick edit that value/clear out that value. So in my request, I can reference the variable, as opposed to a previous response directly, but still optionally source that value from a response.

sylann commented 4 years ago

I think the need here is to have the ability to quickly change the state of "being authenticated".

If we have a endpoint /login that returns '{ "token": "super token" }' And we want to automatically add the token to the Authorization header.

We could create a variable token that would have a Response template tag as value. The template tag would be configured like so:

image

Problem is, this variable will only ever be usable if the response body exists and contains the demanded path.

With this solution, it is not possible to disconnect, because you will essentially not be able to make a call without a token.

So there are 3 hacky solutions:

  1. Have another variable named token__invalid (self explanetory), and manually choose which token to use in a request.
  2. Have a wrapper variable token used everywhere, and change what this variable points to (either token__valid or token__invalid)
  3. Somehow find a way to send an "invalid token" with the same response... not going to try that anytime soon.
sturose commented 4 years ago

Just for reference, the way this is handled in Postman is closest to option 2. You simply set a variable's value when processing a response (as opposed to referencing a response when processing a request). That allows the user to then freely clear out the value whenever they choose. I haven't found a way to do that using insomnia. Insomnia's templating solution seems more complicated and less flexible.

sylann commented 4 years ago

@sturose I remember using the "test" panel to save the token in the environment on login response success. I know I could do that similarly with insomnia plugins, but the documentation is a little terse for now, I've tried some things but - jokes aside - my nodejs is a little rusty.

sturose commented 4 years ago

I guess that's kind of my overall point. Every developer understands defining a variable and setting it's value. It's so much easier than the template system that insomnia has, in my opinion. I've used postman for years, but I want to switch to insomnia. There's just a few things that would make it's use easier for me. Right now, it feels like variables are very inflexible, unless you want to open up and edit the json manually.

zigfreid5 commented 4 years ago

I have a similar use case to present:

I use request chaining to populate the form parameters of a "save" request from a "get" request. Depending on the item, some parameters are not present in the "get" request, and those values are optional in the "save" request. It would be nice if when the JSONPath returns no results, a default value can be provided, or ignore the parameter altogether. Like some kind of null coalescing handler.

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.