0

在 Postman 调用中,如何随着时间的增加更新请求的 json 正文中的变量值。我需要调用端点 2048 次。每个呼叫的 end_time 应该有 5 分钟的差异。我无法将值转换为正常时间格式。

我写了这个:

var moment = require("moment");
var t = pm.variables.get("t");
pm.environment.set('t', moment().add(1000, 'seconds').valueOf(t));
console.log("t", t);

我看到一个错误:

{
    "ErrorCode": "1100",
    "Message": "request.end_time: Error converting value \"1581351445025\" to type 'System.TimeSpan'. Path 'end_time', line 10, position 29."
}

样品请求:(在正文中)

{
  "monday": true,
  "tuesday": true,
  "wednesday": true,
  "thursday": true,
  "friday": true,
  "saturday": false,
  "sunday": false,
  "start_time": "7:30:00",
  "end_time": "{{t}}",
  "start_date": "2020-01-23",
  "end_date": "2020-05-23"
}
4

1 回答 1

0

Pre-req脚本视图中

var moment = require("moment");
var t = pm.variables.get("t");
console.log("t: " + t);
var newT = moment().add(1000, 'seconds').valueOf(t); 
console.log("newT: " + newT);
postman.setEnvironmentVariable("newT", newT);

然后您的请求正文应该更改为使用新变量{{newT}}

不知道为什么,但 usingpm.environment.set根本没有设置环境,但postman.setEnvironmentVariable似乎有效。

于 2020-02-12T16:18:43.333 回答