问题
I have a scenario where i need to pass the body values to an environment variable and use those in another API. In Postman
Below is the body,
{
"firstName" : "Firstname",
"lastName" : "lastname",
"email" : "{{timestamp}}@test.com",
"password" : "{{timestamp}}",
"country" : 8l16
}
Below is the Pre-req script,
postman.setEnvironmentVariable("timestamp", (new
Date).getTime());
// I have copied the Bodyand paste it in a variable called Obj in
Pre-req
// Then i used the below script to get the body
pm.environment.set("rawBody", JSON.stringify(obj));
But the environmental values of timestamp , email and password is coming as below. The timestamp value is correct and other two are wrong.
timestamp = 1566076106769
email = {{timestamp}}@test.com
password = {{timestamp}}
The timestamp value is not getting substituted in email and password,i want the environmental variable value to set as,
Expected values,
email = 1566076106769@test.com
password = 1566076106769
So how can i assign the body element value to an environment/global variable to use in another API call?
回答1:
Easy. You have set the environmental variable but never got it. "{ }" doesn't work in code of Tests and Pre-request Script. Make it like this:
const timestamp = pm.environment.get('timestamp');
email = `${timestamp} @test.com`;
password = timestamp;
来源:https://stackoverflow.com/questions/57541838/get-the-values-of-body-to-variables