56

I'm trying to update the header for my apis with a sif token that is retrieved from another login call. I know how to do this in Postman. There I go to the Tests tab and add something like this for the login api, which would set my global variable.

var data = JSON.parse(responseBody);
postman.setGlobalVariable("SIF_TEACHER", data.sifToken);

I've read this tutorial from the Insomnia official support page but can't really understand it and couldn't find any other doc on chaining requests there.

Thank you.

Francislainy Campos
  • 3,462
  • 4
  • 33
  • 81

3 Answers3

118

In your workspace press CTRL+E to open "Manage Environments" window

Add a variable like "token" to the environment enter image description here

Put a response function (teal f) as value of this variable by pressing CTRL+SPACE. Select one to your liking from the dropdown, in your case "Response => Body Attribute" should work well.

This will open a "Tag" form, like this one: enter image description here

Select your login request and filter the response json or xml for the value containing your token value, f.e. $.access_token. Probably set trigger behaviour to "When Expired" too.

You can now access this variable anywhere in your workspace for other requests by pressing CTRL+SPACE in any form field and selecting the variable (purple x).

If you only need this for one request, you can skip setting up the environment variable for this and directly put the function where you need it, same way as described before.

Frank
  • 1,535
  • 1
  • 11
  • 13
4

You can check the link, in the comments there is a mini clip with the indications

https://github.com/Kong/insomnia/issues/2744

SHdez96
  • 59
  • 2
  • 4
    Please read https://meta.stackoverflow.com/tags/link-only-answers/info "[Link rot](https://en.wikipedia.org/wiki/Link_rot)" "[Your answer is in another castle: when is an answer not an answer?](https://meta.stackexchange.com/q/225370/153968)" – the Tin Man Sep 28 '21 at 21:29
2

There is a plugin that allows you to have variables which you can set its value from different request an use them in others. This is great for when you want to chain requests but you have multiple possible parents and don't want to duplicate the child request, for example you could have "Login with A" and "Login with B" and both save to id, then you can have a "Get info" with the id.

Setting a variable is done using an special tag in the header of the request ("Save variable") and then use its value wherever you want with the "Variable" tag.

You can see more about the plugin in https://insomnia.rest/plugins/insomnia-plugin-save-variables .

PhoneixS
  • 10,574
  • 6
  • 57
  • 73