I am trying to set an env variable with a query param received in the response.
i am sending a POST request and in the response I am getting a resource id i need to use in the next request (therefore need to add it as an env variable).
the response Header:
location →
https://mmmmmmmm.nnnnnnnn.xxxxxx/oauth-signin?client_id=bbbnnnmmmm-bbnn-46ba-a287-nnhhyyy&redirect_uri=https://mmm.nnnn.nnnn/galaxy-backend/redirect/oauth/token&response_type=code&state=kYIL97&protocol=berlin-v13&consent_id=5d1372ebfb0b3a09b2ea4ddb&scope=accounts.accountDetails.read+accounts.transactions.read+accounts.balances.read
i am trying to retrieve the value form the query param 'consent_id' and set it in env variable.
my env variable is named 'ConsentId'.
i tried:
pm.environment.set('ConsentId', JSON.stringify(pm.response.header.getQueryString("consent_id")));
No luck.
Would appreciate help with how to write it so the script will get the field from the a url param in the header and place it in the env variable.
To get ConsentId parameter value in Location response header and set it as environment variable in Postman:
var key = 'consent_id';
var regex = new RegExp('[?&]' + key + '(=([^&#]*)|&|#|$)');
var redirectURL = postman.getResponseHeader('Location');
var matchResult = regex.exec(redirectURL);
var consentId = decodeURIComponent(matchResult[2].replace(/\+/g, ' '));
pm.environment.set('ConsentId', consentId);
BTW, to make above code work well, "Automatically follow redirects" option should be turned off:
Note: I failed to extract Location header from pm.response.headers, that's why postman.getResponseHeader() is used in above code.
Related
I am trying to run a script where postman sends a Get request, and if the get response contains a certain variable to then run a Post request. How do i do this?
(also is there a way to run a get request hourly)
In pre-requisite add:
// set initial value
const method = pm.variables.get("method")
// set initial value as GET if method is undefined
method ? null : pm.variables.set("method", "GET")
// Set this as method
pm.request.method =method
in test script add :
// the condition check
if (pm.response.json().somevalue === "somevalue") {
//then change the method
pm.variables.set("method", "POST")
//call the same request again using setNExtRequest
// pm.info.reqeustName gives current request's name
postman.setNextRequest(pm.info.requestName)
}
This is my response body, I want to get the value of status by using an environment variable.
{
"success":[
{
"code":"200",
"message":"Success",
"details":"Station was retrieved successfully."
}
]
}
i can get value of status code as: jsonData.success.code,
But instead of this, I'm doing it like,
I m setting an environment variable named 'sh' n I gave value as "code"...
pm.environment.set("sh","code");
var s = pm.environment.get("sh")
jsonData.status.success[0].s
By doing like this I'm not able to get the path...Any solutions?
Thanks in advance.
It's hard to tell what your actual response body is but this should probably work:
let statusCode = pm.response.json().success[0].code
pm.environment.set("statusCode", statusCode)
Ensure that you create an environment file before running the request.
I am trying to write some tests for Postman. Many of the requests require an API key that gets returned by an initial GET request.
To set something hard-coded that is not dynamic, it looks like the test code is of the form
let variable = pm.iterationData.get("variable");
console.log("Variable will be set to", variable);
How do I set a return value as a global variable and then set that as a header parameter?
#Example if you setting ApiToken that is dynamic.
Under Tests tab in postman.
Put the following code.
var jsonData = JSON.parse(responseBody);
postman.setEnvironmentVariable("Token", jsonData.token);
Under specific environment put variable name as "Token" and current value will be set automatically.
access the variable using {{variable_name}}
#Example: {{token}}
You can specify that variable value in the request Headers by using the {{var_name}} syntax. Instead of any hardcoded value that you may have been using.
You would previously have had to set the value using the pm.globals.set()syntax.
If you do not have the SDK active (monthly payment required to Postman). You can retrieve the values through __environment and __globals.
//Use of environment
postman.setEnvironmentVariable("my_value", "This is a value");
//...
let myValueVar = postman.__environment["my_value"];
console.log("Variable value is:", myValueVar);
//shows: Variable value is: This is a value
//Use of Globals
postman.setGlobalVariable("my_value2", "This is a value of globals");
//...
let myValueVar2 = postman.__globals["my_value2"];
console.log("Variable value is:", myValueVar2);
//shows: Variable value is: This is a value of globals
Also you can see your globals variables in Environments->Globals of your Posmant client.
I need to set some response headers within server.onPreHandler ext method.
There are 2 scenarios, where I need this happen when user sends an API request to my route end point.
1) In success scenario, I need to set headers and let process continue further down life cycle
2) In error scenario (where user has not provided a required field), I need to set headers and return immediately to user with appropriate error info.
In both of these scenarios, I would like to set response headers.
In the 2nd scenario above, I am able to invoke reply.response('error') and then set response header to it using response.header('x', 'value'). However, in the 1st scenario, where before calling reply.continue() I am trying to set header using request.response.header('x', 'value), I get response null error.
Please help
Thanks
Ramesh
I'm able to change response headers like this. Have you tried this way?
// at your onPreResponse ext body
const response = request.response;
if (request.response.isBoom) {
response.output.headers['x'] = 'value';
} else {
response.header('x', 'value');
}
I am using some of the auto generated parameters in my request body in a postman request(i.e: {{$guid}}).
I would like in my test to retrieve the request that was sent to the server to compare what this variable value was, and what the response parroted back to me me in my request.
for example, my request's body looks like this:
{
"Description": "testing this {{$guid}}"
}
and I would in the tests be able to do:
var req = JSON.parse(requestBody);
var resp = JSON.parse(responseBody);
test['description should match'] = req.Description === resp.Description;
is this doable?
This is possible.
But you have several small syntax errors.
To access the request body data use:
var req = JSON.parse(request.data);
I named the variable req to not be confused with the predefined request variable. You can log the result like this:
console.log(req.Description);
In the tests tab make sure you reference the correct variable tests with "s". Also you pass the test case name as a string e.g. "description should match".
var res = JSON.parse(responseBody);
console.log(res.Description);
tests["description should match"] = req.Description === res.Description;