Postman 获取body to变量的值
我有一个场景,需要将主体值传递给环境变量,并在另一个API中使用这些值。邮递员 下面是身体,Postman 获取body to变量的值,postman,postman-collection-runner,Postman,Postman Collection Runner,我有一个场景,需要将主体值传递给环境变量,并在另一个API中使用这些值。邮递员 下面是身体, { "firstName" : "Firstname", "lastName" : "lastname", "email" : "{{timestamp}}@test.com", "password" : "{{timestamp}}", "country" : 8l16 } 下面是Pre-req脚本 postman.setEnvironmentVariable("times
{
"firstName" : "Firstname",
"lastName" : "lastname",
"email" : "{{timestamp}}@test.com",
"password" : "{{timestamp}}",
"country" : 8l16
}
下面是Pre-req脚本
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));
但时间戳、电子邮件和密码的环境值如下。时间戳值正确,其他两个值错误
timestamp = 1566076106769
email = {{timestamp}}@test.com
password = {{timestamp}}
时间戳值在电子邮件和密码中未被替换,我希望环境变量值设置为
期望值
email = 1566076106769@test.com
password = 1566076106769
那么,如何将body元素值分配给环境/全局变量,以便在另一个API调用中使用呢?简单。您已经设置了环境变量,但从未得到它。 “{}”在测试代码和预请求脚本中不起作用。 这样做:
const timestamp = pm.environment.get('timestamp');
email = `${timestamp} @test.com`;
password = timestamp;
简单。您已经设置了环境变量,但从未得到它。 “{}”在测试代码和预请求脚本中不起作用。 这样做:
const timestamp = pm.environment.get('timestamp');
email = `${timestamp} @test.com`;
password = timestamp;
你用的是什么版本的邮递员?除了pm.environment.set(“rawBody”,JSON.stringify(obj))行之外,您的代码对我来说正如期工作;你用的是什么版本的邮递员?除了pm.environment.set(“rawBody”,JSON.stringify(obj))行之外,您的代码对我来说正如期工作;