Francislainy Campos
Francislainy Campos

Reputation: 4118

How to chain requests using Insomnia (get token from login api to use as header for another api)

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.

Upvotes: 78

Views: 65326

Answers (4)

gOliveiraC
gOliveiraC

Reputation: 43

I got the solution by adding an pre-request script like this:

const tokenUrl = '';


const requestBody = {
  "login": "",
  "password": "teste"
};


const response = await fetch(tokenUrl, {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json'
    },
    body: JSON.stringify(requestBody)
});

if (response.ok) {
    const data = await response.json();
    const token = data.token;

    insomnia.environment.set('authToken', token);

} else {
    throw new Error('Erro to generate token: ' + response.status);
}

and then on Auth tab put the variable authToken. enter image description here

Upvotes: 0

PhoneixS
PhoneixS

Reputation: 11016

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 .

Upvotes: 2

SHdez96
SHdez96

Reputation: 69

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

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

Upvotes: 5

Frank
Frank

Reputation: 2055

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.

Upvotes: 170

Related Questions