Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How automatically getting token in Postman

I use the Postman desktop app for web API testing. I have a lot of controllers and for each need a token. First I get Bearer token and then copy it to other requests. This token have limit time. Can I get token automatically and then automatically set it to all others requests ?

like image 651
Сергей Avatar asked Jun 18 '18 10:06

Сергей


People also ask

How to use authtoken in Postman?

Write below code in tests tab in postman for your login request. Then edit your collection and set your env authToken inside. Show activity on this post. You can save and re-use the token's using the Token Name from Postman. You can select it from the available token list.

How do I add a token to a postman variable?

This would take us to this part of postman. We start our editing with the variables tab. We add a new variable called token. We plan on updating this variable automatically once a user is authenticated and a response token is sent back to us. This value is what would then be passed as value in the Bearer token.

Can I use OAuth2 with Postman collection?

On my project, we have an API that grants and uses OAuth2 access tokens for authentication (with no refresh token). In our Postman Collection, we can take advantage of collection-level authorization so that we don’t have to configure it request by request.

How do I make a call to an API using postman?

Here we will use Postman to make a call to our API with the correct params, parse the response and set a variable with the Bearer Token. Of course you will need to modify to fit your needs, but below is what worked for me. I get a JSON response back from the API with the token in the access_token key.


Video Answer


1 Answers

ok, I just used Environments in postman.

1 - create new Environment with token. enter image description here

2 - add test after auth request like this :

var jsonData = JSON.parse(responseBody);
var token = jsonData._token;
postman.setEnvironmentVariable("token", token);

3 - just set {{token}}

enter image description here

And of course you can set token before request if you use Pre-request Script in one of requests.

like image 136
Сергей Avatar answered Oct 14 '22 01:10

Сергей