restpostmanweb-api-testing

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 ?


Solution

  • 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.