Skip to content

Instantly share code, notes, and snippets.

@k8scat
Created May 12, 2021 10:52
Show Gist options
  • Select an option

  • Save k8scat/53574d35ea4505884fca2b84661805dd to your computer and use it in GitHub Desktop.

Select an option

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