I'm trying to setup a Postman Pre-request to get an OAuth token before each request I make to my service.
I've tried to follow various examples and guides related to this and each seems pretty straightforward, though I still have issues with my script as the pre-request script fail.
I'm afraid this is somehow related to the fact that the endpoint I need to call to get the token is on http protocol and not https, as in the Postman console I'm always getting 2 separate requests to the same url, one on https and one on http.
Here is my pre-request script
pm.expect(pm.environment.has('host')).to.be.true;
pm.expect(pm.environment.has('client_id')).to.be.true;
pm.expect(pm.environment.has('client_secret')).to.be.true;
pm.expect(pm.environment.has('username')).to.be.true;
pm.expect(pm.environment.has('password')).to.be.true;
var options = { method: 'POST',
url: 'http://' + pm.environment.get("host") + '/api/oauth/token',
headers:
{
Authorization: 'Basic '+btoa(pm.environment.get("client_id")+':'+pm.environment.get("client_secret")),
'Content-Type': 'application/x-www-form-urlencoded'
},
form:
{
grant_type: 'password',
username: pm.environment.get('username'),
password: pm.environment.get('password'),
}
};
pm.sendRequest(options, function(err, response) {
console.log(response.json())
});
And here what I see in my Postman console
As you can see from the picture the first request fails and the second one generates a 401 HTTP response.
If I run the same request outside of the pre-request script as a "regular" postman request on http everything works fine, but on https I get an error as the request is not even made
How can I make my pre-request script working fine?