Description
So, I have a Pre-request script in Postman which runs and gets a API_token, which in turn sets the env variable "api_token". I have set this "api_token" using the script specified in the next section.
Requirement
I want the behaviour to be such.
- I click the send button.
- The Pre-Request script handles whole request and the default behaviour is prevented.
- The result of the request is outputted as if the default request was sent. ( optional. brownie points if you can get this :D )
I tried the following script :
// setting the header body
const options = {
url: 'https://api/v1/login',
method: 'POST',
header: {
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body: {
mode: 'raw',
urlencoded : [
{ key: 'username', value: 'admin'},
{ key: 'password', value: 'Admin@123'},
]
}
};
// sending the request :D
pm.sendRequest(options, function (err, response) {
pm.environment.set("api_token", response["id"]);
});
// throw new Error("Error : access denied"); I Tried to prevent any further execution;
return;
Shortcomings
- Although, It does set the env variable, but it also sends the default request which in turn creates 2 api_tokens for me ( which I want to prevent)
- It does not display the response I generated with my pre-request script.