获取body的值到变量 [英] Get the values of body to variables
问题描述
我有一种情况,我需要将主体值传递给环境变量并在另一个API中使用它们。在邮递员中
I have a scenario where i need to pass the body values to an environment variable and use those in another API. In Postman
下面是尸体,
{
"firstName" : "Firstname",
"lastName" : "lastname",
"email" : "{{timestamp}}@test.com",
"password" : "{{timestamp}}",
"country" : 8l16
}
以下是Pre-req脚本,
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));
但是timestamp,电子邮件和密码的环境值如下。时间戳记值正确,其他两个错误。
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,
期望值,
email = 1566076106769@test.com
password = 1566076106769
那么,如何将body元素值分配给要在另一个API调用中使用的环境/全局变量?
So how can i assign the body element value to an environment/global variable to use in another API call?
推荐答案
简单。您已经设置了环境变量,但是没有得到。
{}在测试和预请求脚本的代码中不起作用。
像这样:
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;
这篇关于获取body的值到变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!