Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

insomnia

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 by going to the Tests tab and adding something like this there for the login api which would set my global variable.

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

However, I read this tutorial from their official support page but can't really understand it and couldn't find any other doc on chaining requests. If someone would be so kind to share more detailed steps to do this or point me to some doc on the subject please?

Thank you very much.

like image 843
Francislainy Campos Avatar asked Aug 21 '20 09:08

Francislainy Campos


People also ask

How do you pass the bearer token in header insomnia?

Using a GUI app If you're using Insomnia, start by creating a new GET request (click the plus icon, or use keyboard command+N or control+N on Windows/Linux). In the “Auth” dropdown menu, select “Bearer Token”. Type in your access token in the “TOKEN” field, and type the word “Bearer” in the “PREFIX” field.

How do I automatically set the access token in insomnia?

To set the bearer token, we can click on the Bearer tab and enter Response ⇒ Body Attribute for the token. To edit the tag, click on it. In the Edit Tag screen, select Post GetAccessToken for the request.


1 Answers

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.

like image 148
Frank Avatar answered Oct 21 '22 04:10

Frank