Use authentication token in follow-up requests in Postman -
my app api requires authentication via authentication token. in short, send request /authentication
endpoint , responds json object containing token, like:
{"token": "xxxxxxxxxxxxxxxxxxxxxx"}
every other api endpoint in our application requires authentication
header containing token. now, in postman it's possible authentication request, copy token, open next endpoint , paste authentication
header in manually. becomes tedious , time-consuming when testing lots of endpoints.
is there way have postman save , automatically add authentication
token 1 request in follow-up requests?
even better, postman automatically send /authentication
request prior of other requests?
postman allows wide variety of options when crafting api requests.
in case, can create global variable token when receive by:
var jsondata = json.parse(responsebody); postman.setglobalvariable('token', jsondata.token);
this go in tests
tab, in order execute script after request has been completed.
now, global variable token
set , can accessed using {{token}}
syntax in following api requests make.
i'll demonstrate regarding same, similar example:
1. save data of latitude , longitude global variables lat
, long
. 2. reuse data referring name of variable, i.e. lat
, long
enclosing them within curly braces {{lat}}
, {{long}}
.
- you can manage these global variables, clicking on gear icon in top right corner, , selecting manage environments opening globals tab.
- tip: can also, save request obtain token collections, each time, don't have craft url obtain token.
Comments
Post a Comment