获取body的值到变量

时间:2019-08-18 05:16:23

标签: postman postman-collection-runner

我有一种情况,我需要将主体值传递给环境变量并在另一个API中使用它们。在邮递员中

下面是身体

{
  "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,email和password的环境值如下。时间戳记值正确,另外两个错误。

 timestamp = 1566076106769
 email = {{timestamp}}@test.com
  password = {{timestamp}}

时间戳值未替换为电子邮件和密码,我希望将环境变量值设置为

期望值,

 email = 1566076106769@test.com
 password = 1566076106769

那么我如何将body元素值分配给环境/全局变量以在另一个API调用中使用?

1 个答案:

答案 0 :(得分:0)

容易。您已经设置了环境变量,但是没有得到。  “ {}”在测试预请求脚本的代码中不起作用。 像这样:

const timestamp = pm.environment.get('timestamp');
email = `${timestamp} @test.com`; 
password = timestamp;