Insomnia: [Question] Are there any way to `setEnvironmentVariable` like postman

Created on 16 May 2017  路  15Comments  路  Source: Kong/insomnia

Overview

  • Insomnia Version: 5.0.20
  • Operating System: MacOS 10.11.4 (15E65)
  • Summary: _(A few sentences describing the issue. Try to include as much detail as possible.)_

I preRequest to get accessToken, and at next request I use the token, like postman

postman.setEnvironmentVariable("accessToken", 'Bearer ' + data.accessToken);

image

Most helpful comment

I think that chaining-requests is nice, but sometimes I just want to make this even more powerful. Maybe I want to save the data to a variable so I can reference it by name, rather than by a random tag, or maybe I want to compose several response bodies into one.
Do you have plans to implement this?
I also want to be able to run some scripts to make checks on the responses.

All 15 comments

Not at the moment, but #210 (Request Chaining) will provide a way to do this. Instead of setting a token variable after a login request (for example), you would reference the login request's response from an environment variable with something like this:

{% response 'req_123', 'body', '$.accessToken' %}

Besides #210, there will also be a plugin system (#51) later on that will make scripting like this possible.

Going to close this issue, but feel free to respond with any questions or comments you may have.

Does this mean that I will be firing off multiple, redundant (and possibly impotent) requests?

For example, if I define three requests which each require authentication (which is commonplace), and the result of two requests are required for a third one, is this what Insomnia would do underneath the hood for the third request (say, mapIcecreamsRequest):

mapIcecreamsRequest
authRequest > getAllChocolateIcecreams (w. auth token) > authRequest (redundant, since still logged in, but request new token anyway) > getAllVanillaIcecreams > authRequest (again redundant) > mapIcecreamsRequest (using response from chocolate and vanilla requests)

If this is what is happening, please let me manage request chaining myself by allowing us to simply set environment variables ourselves using the response value (or header, or cookie, or whatever).

Check out the docs https://support.insomnia.rest/article/43-chaining-requests

The referenced requests are not executed automatically. Insomnia pulls the value out of the last response made

I know this thread is already closed, but It is even a possible feature or it is disscarded?

I think that chaining-requests is nice, but sometimes I just want to make this even more powerful. Maybe I want to save the data to a variable so I can reference it by name, rather than by a random tag, or maybe I want to compose several response bodies into one.
Do you have plans to implement this?
I also want to be able to run some scripts to make checks on the responses.

My use case would be the following:
I have _n_ possible requests that create a _ticket_ at the database and return the created id.
Later, I also have a number of requests that manipulate the created ticket and requires its id.

With this scenario, I have to declare the ticket_id as an environment variable and update it manually according to the result of the chosen create ticket request.

The chaining requests are useful and serves me very well, but it lacks on these multiple requests same variable scenario.

Like the above, we have a bunch of requests that cause tasks to be created and return the task's id. Then we have one single request that checks the given task id.

Chaining requests doesn't work here as I can only assign one single request as a source, not 100.

So I want to execute the creating request and then be able to execute the check request a few times with that task id.

I frequently have the problem that @diegosps references, so I'd prefer a way to chain requests with looser coupling, such that multiple initial requests could be the source for an input to multiple secondary requests -- the ticket example specified is a fine one. Having a way to pass it through an environment variable is certainly one way of making that happen, although I don't so much care about the specifics of the mechanisms as long as there's a way to do it.

So -- anything that allows me to, say, create a new resource from one of several request, and then have a chained request to manipulate that resource from whichever one of the requests created it, that's what I'm looking for.

Wouldn't be awesome to have an UI that allows you to not only make this multiple chained requests but also visualize them? Something like this:

image

It looks like this is "dead", and that's really, really sad. There's really no way to "setEnvironmentVariable" from a response? I see that in a response, you can use "getEnvironmentVariable", but that's not helpful for this.

As has been well-said previously, it's critical to be able to set an Environment variable from data in the response of a request. But there doesn't even seem to be a way to write a plugin to make this happen. Is that in the works?

It looks like this is "dead", and that's really, really sad. There's really no way to "setEnvironmentVariable" from a response? I see that in a response, you can use "getEnvironmentVariable", but that's not helpful for this.

As has been well-said previously, it's critical to be able to set an Environment variable from data in the response of a request. But there doesn't even seem to be a way to write a plugin to make this happen. Is that in the works?

Il looks doable for me now. If you take a look at the documentation it explains how to do it.
You can set an environment variable as a part of the response of a request.
Sadly, I didn't figure out how to use two different routes for the same variable.

It looks like this is "dead", and that's really, really sad. There's really no way to "setEnvironmentVariable" from a response? I see that in a response, you can use "getEnvironmentVariable", but that's not helpful for this.
As has been well-said previously, it's critical to be able to set an Environment variable from data in the response of a request. But there doesn't even seem to be a way to write a plugin to make this happen. Is that in the works?

Il looks doable for me now. If you take a look at the documentation it explains how to do it.
You can set an environment variable as a part of the response of a request.
Sadly, I didn't figure out how to use two different routes for the same variable.

That's old news, unfortunately. We need the opposite: We want N number of different requests to each be able to independently push a change to an environment variable, based on the response, when they're executed. The "response chaining" docs talk about being able to pull data from the response of a single request.

There are presently 2 lame workarounds which would allow you to use "request chaining" in its present form:

  1. Pretend that "Workspaces" are "Environments". Then in each "Environment", you have a single authentication request saved, and a copy of all of the requests you want to test/use with that account. Then when you switch between actual environments (e.g. local, beta/staging/uat, production, etc.), you instead switch between workspaces. It's super tedious.
  2. Have a single authentication request where the credentials are an environment variable. Then when you execute that request, you have to change the underlying data that the variable is pointing to.

@Kirjy I second. In my case I need to get authentication from another server then use it to make a request to REST service, It is doable as you mentioned.

My Environment Variable:
```
{
"KEYCLOAK_ACCESS_TOKEN": "{% response 'body', 'req_5ab4cb4fdd7e4935b9f80e4ae8d341b3', 'b64::JC5hY2Nlc3NfdG9rZW4=::46b', no-history %}"
}
````

Then for the Authentication Header:
image

So I just need to re-request the Auth endpoint if the token expired to get a new one or if no history has been made yet it is automatically triggered.

For 2 request you can just chain it.
1st Request => 2nd Request => Final Request

If 2nd request doesn't need anything from 1st request just put is in any custom header just to make a request.

@jariesdev Sure -- now imagine that you want the keycloak access token variable to be filled from any one of N source requests. Whichever request was last made to get a key cloak access token is now populated in the variable. That's the piece we currently can't do.

This seems like such a big miss for insomnia. I have a lot of payload examples that I want to save and send off then save an id from the response to a global variable. That way I can fetch and do other things referencing that one variable that multiple calls can set.

Right now I have to make the 2-3 calls that I need to chain, put them in a folder then duplicate the folder, then go into every chanined call and change it to grab the id out of the new call response body, vs the initial calls saving the response body to a single variable.

Was this page helpful?
0 / 5 - 0 ratings