Last active
July 26, 2024 21:00
-
-
Save franckweb/a3c5efc1d498c8df1b4b31c556f4bbaa to your computer and use it in GitHub Desktop.
Postman script to automatically get token and assign it to environment variable
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Postman script to automatically get token and assign it to environment variable | |
* - Create new endpoint (GET, POST, etc) | |
* - Create an env variable called "access_token" | |
* - Add this script in Tests tab in Postman for your newly created endpoint | |
* - To debug you can use Postman Console (Alt + Ctrl + C) | |
*/ | |
const tokenUrl = 'https://mywebsite.com/auth/token'; | |
const apiKey = "123FJSKAHFSAJFHSAFKJSAFHASSAFA"; | |
const apiKeySecret = "456FSAFLKAFAKFJLSFSAJFLKSAJFLKA"; | |
const getTokenRequest = { | |
method: 'POST', | |
url: tokenUrl, | |
header: { | |
'MY-CUSTOMER-KEY': apiKey, | |
'MY-CUSTOMER-SECOND-KEY': apiKeySecret | |
}, | |
body: {} | |
}; | |
pm.sendRequest(getTokenRequest, (err, response) => { | |
const jsonResponse = response.json(); | |
const newAccessToken = jsonResponse.token; | |
// create a postman environment variable called "access_token" | |
// and next code line will set it with retrieved token | |
pm.environment.set('access_token', newAccessToken); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment