Skip to content

Instantly share code, notes, and snippets.

@sebastianneubert
Created February 15, 2022 10:43
Show Gist options
  • Save sebastianneubert/3b81c4554d4080936629e61d730042e9 to your computer and use it in GitHub Desktop.
Save sebastianneubert/3b81c4554d4080936629e61d730042e9 to your computer and use it in GitHub Desktop.
Postman prerequest script

Postman prerequest script

used variables

base_url

the url of the API

username & password

username/password which will authenticate against the API to get a token

usage

in your request you can define the header Authorization: Bearer {{auth_token}}

// add this in the postman collection root in the prescript tab
pm.sendRequest({
//
url: pm.environment.get('base_url') + '/core/api/auth/login',
method: 'POST',
header: {
"Content-Type": "application/json",
"Accept": "application/json"
},
body: {
mode: 'raw',
raw: JSON.stringify({"username": pm.environment.get('username'), "password": pm.environment.get('password')})
}
}, function(err, response) {
// check, what the body of your result will look like
let result = response.json();
pm.environment.set('auth_token', result.token);
pm.environment.set('refresh_token', result.refresh_token)
}
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment