Created
May 12, 2021 10:52
-
-
Save k8scat/53574d35ea4505884fca2b84661805dd to your computer and use it in GitHub Desktop.
Pre-request Script in Postman help me get and refresh the access_token.
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
| // https://learning.postman.com/docs/writing-scripts/script-references/postman-sandbox-api-reference/#sending-requests-from-scripts | |
| const url = pm.environment.replaceIn("{{base_url}}/some/api") | |
| const req = { | |
| url: url, | |
| method: 'POST', | |
| header: { | |
| 'Content-Type': 'application/json', | |
| 'X-Foo': 'bar' | |
| }, | |
| body: { | |
| mode: 'raw', | |
| raw: JSON.stringify({ | |
| key: 'this is json' | |
| }) | |
| } | |
| }; | |
| pm.sendRequest(req, function (err, response) { | |
| if (response.code === 200) { | |
| const data = response.json() | |
| pm.environment.set('access_token', data.access_token) | |
| return | |
| } | |
| console.log(`Error: ${response}`) | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment