如何在 POST API 请求中添加当前日期时间?
How to add current datetime in POST API request?
我希望开始日期和结束日期在当前日期时间。我不知道这是否可能。我需要它,因为我想每天使用我的管道触发数据。
您可以在请求的预请求脚本中创建一个环境变量,然后在正文中使用该变量
var now = new Date();
var timestamp = now.toISOString(); //or whatever format you want.
pm.environment.set("timestamp", timestamp);
您可以从请求正文中删除 "start"
和 "end"
,然后使用 Postman 的 Pre-request Script 部分([=15 旁边=]Body), 添加以下行:
// 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` });
您可以使用 moment
让这对您来说更容易。将此添加到 pre-request script
:
let moment = require('moment');
pm.variables.set('startOfDay', moment().utc().startOf('day').format('YYYY-MM-DD HH:mm:ss'));
pm.variables.set('endOfDay', moment().utc().endOf('day').format('YYYY-MM-DD HH:mm:ss'));
在需要的地方使用 {{startOfDay}}
和 {{endOfDay}}
变量。
我希望开始日期和结束日期在当前日期时间。我不知道这是否可能。我需要它,因为我想每天使用我的管道触发数据。
您可以在请求的预请求脚本中创建一个环境变量,然后在正文中使用该变量
var now = new Date();
var timestamp = now.toISOString(); //or whatever format you want.
pm.environment.set("timestamp", timestamp);
您可以从请求正文中删除 "start"
和 "end"
,然后使用 Postman 的 Pre-request Script 部分([=15 旁边=]Body), 添加以下行:
// 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` });
您可以使用 moment
让这对您来说更容易。将此添加到 pre-request script
:
let moment = require('moment');
pm.variables.set('startOfDay', moment().utc().startOf('day').format('YYYY-MM-DD HH:mm:ss'));
pm.variables.set('endOfDay', moment().utc().endOf('day').format('YYYY-MM-DD HH:mm:ss'));
在需要的地方使用 {{startOfDay}}
和 {{endOfDay}}
变量。