【问题标题】:Get the values of body to variables获取body的值到变量
【发布时间】:2019-12-23 19:03:02
【问题描述】:

我有一个场景,我需要将主体值传递给环境变量并在另一个 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 调用中使用?

【问题讨论】:

  • 您使用的是哪个版本的 PostMan?您的代码按预期为我工作,除了行 pm.environment.set("rawBody", JSON.stringify(obj));

标签: postman postman-collection-runner


【解决方案1】:

简单。您已经设置了环境变量,但从未得到它。 "{ }" 在 TestsPre-request Script 的代码中不起作用。 变成这样:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2014-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-25
    • 2022-01-17
    相关资源
    最近更新 更多