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 ?
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.
Following steps:
1. Create an environment on the Postman. Example: test_env
2. Create an environment variable. Example: jwtToken
3. Select the environment and go to the token API
4. Set Content-Type as application/json in Headers
5. goto the Tests option in Postman and write the script:
if(pm.response.code === 200) {
var my_token_response = JSON.parse(responseBody);
postman.setEnvironmentVariable("jwtToken", "Bearer "+my_token_response.access);
}
Write below code in tests tab in postman for your login request.
if(pm.response.code === 200) {
pm.environment.set('authToken', pm.response.json().token)
}
Then edit your collection and set your env
authToken
inside.
You can save and re-use the token's using the Token Name from Postman. You can select it from the available token list.
One of the many cases are.
- Request for a refresh token using the credentials
- Use the refresh token to get an access token
- Use the access token to authenticate the API.
The step 1 sometimes requires us to login to an interface of the API provider and get an authentication code to our callback url. Some API provider's allow us to override this by providing the client_secret key and the client_id as an authorization header and the refresh token as the request parameters and by setting prompt as none. From the documentation.
prompt (optional) none no UI will be shown during the request. If this is not possible (e.g. because the user has to sign in or consent) an error is returned.
https://identityserver.github.io/Documentation/docsv2/endpoints/authorization.html
All you need to know about the identity servers are here.
For me, I updated the token anytime I logged in and these were the steps I took to set up:
- create an environment in postman and a variable (eg token). You can set your initial value.
- Go to the endpoint where you want to update the token and select the test tab
- Write these lines of code and click save, you can actually ignore the test functions:
//set token when status is 200
if(pm.response.code === 200) {
// Extract token from response body
const responseBody = pm.response.json();
const token = responseBody.data.token;
// Save token to environment variable
pm.environment.set('token', token);
}
//You can ignore this
pm.test("Response status code is 200", function () {
pm.expect(pm.response.code).to.equal(200);
});
pm.test("Token should not be empty", function () {
const responseData = pm.response.json();
pm.expect(responseData.data.token).to.not.be.empty;
});
I'm using this code to refresh the environment variables used as storage for API authorization bearers in Postman.
pm.test("Set new EnvVar token", function () {
let authToken = pm.response.json().YOUR_RESPONSE_JSON_AUTH_TOKEN_KEY;
let refreshToken = pm.response.json().YOUR_RESPONSE_JSON_REFRESH_TOKEN_KEY;
pm.environment.set("YOUR_ACCESS_TOKEN_ENV_VARIABLE_NAME", authToken);
pm.environment.set("YOUR_REFRESH_TOKEN_ENV_VARIABLE_NAME", refreshToken);
});
- Create Postman Env Variables to store your Access and Refresh Tokens
- Create an Auth Request
- Put this code inside the Test tab of the Auth Request
- Replace "YOUR_RESPONSE_JSON_..._TOKEN_KEY" placeholders with your real JSON keys (from API auth response)
- Send Auth Request, check it went fine and you have proper Auth Response (Status: 200 OK)
- Check if your Env Variables are filled with values from the Auth Response JSON
- Enjoy your automation using Auth and Refresh tokens from EnvVars :)
Hope that helps
for testing local in postman , Best method to set bearer token for all the request quick and easy way is, first you create a bearertoken with validity for more than 1 year(so it will not ask expired ) and copy ,and click on the postman collection top of the folder , if collection is
commonapi
Loginfolder
- loginapi
- signupapi
- homescreenapi
, like this format , click on top folder name commonapi, and there you can set bearertoken and paste the value , now in loginapi and all the below subfolder apis we can just set inherit auth from parent , so we dont need to create and set everytime bearer token again and again ,and it will save more time.
© 2022 - 2024 — McMap. All rights reserved.