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 ?
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.
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.
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.
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.
ok, I just used Environments in postman.
1 - create new Environment with token.
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}}
And of course you can set token before request if you use Pre-request Script
in one of requests.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With