Skip to content

Instantly share code, notes, and snippets.

@mahammedzkhan
Created May 20, 2022 16:03
Show Gist options
  • Save mahammedzkhan/a29fd0f67847d39abff1c249543ca46d to your computer and use it in GitHub Desktop.
Save mahammedzkhan/a29fd0f67847d39abff1c249543ca46d to your computer and use it in GitHub Desktop.
Pre-request script for authentication in Postman requests
const username = pm.environment.get("authUser");
const password = pm.environment.get("authPass")
const options = {
url: 'https://api.be/v1/auth/token',
method: 'POST',
header: {
'Authorization': 'Basic ' + btoa(username + ':' + password)
}
};
pm.sendRequest(options, function (err, response) {
const responseJson = response.json();
if (responseJson.access_token) {
pm.environment.set("authToken", responseJson.access_token);
pm.globals.set("authToken", responseJson.access_token);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment