Skip to content

Instantly share code, notes, and snippets.

@gauravat16
Last active November 8, 2019 08:35
Show Gist options
  • Save gauravat16/f277d8613e12e7199188a3b9778652ef to your computer and use it in GitHub Desktop.
Save gauravat16/f277d8613e12e7199188a3b9778652ef to your computer and use it in GitHub Desktop.
Postman Security Token Pre-req Script
/*
Header for your request
*/
header = { 'content-type': 'application/json'};
/*
Converts the Request to a Obejct.
*/
var sdk = require('postman-collection'),
url = new sdk.Url(request.url),
urlJson = url.toJSON();
/*
Hostname of the request. This is needed if your Auth Service is located on the same host/environment.
eg - cool-host.domain.com/test-service/some-request
cool-host.domain.com/auth
*/
var host = urlJson['host'][0];
var url = 'https://'+host+'.fabmailers.in/fabuser/agent/login';
if(host === 'localhost'){
url = 'dev-auth-url';
}
request = { username: 'test',
password:'password',
};
/*
You can change the host to a static value if you have just one environment to test.
*/
pm.sendRequest({
url: url,
method: 'POST',
header: header,
body: {
mode: 'raw',
raw: JSON.stringify(request)
}
}, function (err, res) {
performAction(err, res);
});
/*
Extracts token from the response and adds it to the request header.
*/
function performAction(err, res){
console.log(res.json());
var token = res.json().data.token;
console.log("Got token - " + token);
pm.request.headers.upsert({key: 'token', value: token });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment