如何在 POST API 请求中添加当前日期时间?

Azi*_*mal 6 api postman

我希望开始日期和结束日期为当前日期时间。我不知道是否可以。我需要它,因为我想每天使用我的管道触发数据。

请查看此处的图片

der*_*her 8

您可以在请求的预请求脚本中创建一个环境变量,然后在正文中使用该变量

var now = new Date();
var timestamp = now.toISOString(); //or whatever format you want.
pm.environment.set("timestamp", timestamp);
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


sam*_*man 2

您可以从请求正文中删除"start"和,然后使用 Postman 的预请求脚本部分(位于Body旁边),添加以下行:"end"

// Gets current UTC time in the format "yyyy-MM-dd"
const UTCDate = (new Date()).toISOString().split("T")[0];

// Removes manually set values for "start" and "end", if present
pm.request.body.urlencoded.remove(param => param.key === "start" || param.key === "end");
// Adds a parameter "start" set to UTC midnight
pm.request.body.urlencoded.add({ key: "start", value: `${UTCDate}T00:00:00.000Z` });
// Adds a parameter "end" set to just before UTC midnight of the next day
pm.request.body.urlencoded.add({ key: "end", value: `${UTCDate}T23:59:59.999Z` });
Run Code Online (Sandbox Code Playgroud)