如何为邮递员请求生成未来的日期和时间 [英] How to generate future date and time for postman request

查看:47
本文介绍了如何为邮递员请求生成未来的日期和时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

{
    "name": "IronMan",
    "phone_number": "555555555",
    "number_of_guest": 10,
    "tables": [
        2
    ],
    "reservation_start_at": "2020-10-15T10:00:00.861873Z",
    "reservation_end": "2020-10-15T11:00:00.861873Z"
}

我被困在需要生成带有时区的未来日期和时间以发送邮递员请求的步骤.该请求与预订功能有关.

I am stuck at step where I need to generate future date and time with time zone to send a postman request. The request is about a reservation feature.

在请求正文中,一个键值是,

On the request body,a key-value is,

reservation_start_at":2020-10-15T10:00:00.861873Z

reservation_start_at": "2020-10-15T10:00:00.861873Z

我的问题是,如何为每个请求生成未来的日期和时间?

My question is, how do I generate future date and time for each request?

提前致谢.

推荐答案

您可以在 postman 的预请求脚本中使用以下内容:

You can use the following in postman's pre-request script:

var moment = require('moment');

const yearsInTheFuture = 1;
const monthsInTheFuture = 2;
const daysInTheFuture = 3;
const hours = 12;
const minutes = 34;
const seconds = 56;
const milliseconds = 789;
const durationInDays = 7;

const reservation_start_at = moment()
    .add(yearsInTheFuture, 'years')
    .add(monthsInTheFuture, 'months')
    .add(daysInTheFuture, 'days')
    .hours(hours)
    .minutes(minutes)
    .seconds(seconds)
    .millisecond(milliseconds);

reservation_end = moment()
    .add(yearsInTheFuture, 'years')
    .add(monthsInTheFuture, 'months')
    .add(daysInTheFuture + durationInDays, 'days')
    .hours(hours)
    .minutes(minutes)
    .seconds(seconds)
    .millisecond(milliseconds);

console.log("reservation_start_at: " + reservation_start_at);
console.log("reservation_end: " + reservation_end);

pm.environment.set("reservation_start_at ", reservation_start_at);
pm.environment.set("reservation_end", reservation_end);

它将设置环境变量 reservation_start_atreservation_end,然后您可以在请求正文中访问它们:

It will set the environment variables reservation_start_at and reservation_end, which you then can access in the request body:

{
   "name":"IronMan",
   "phone_number":"555555555",
   "number_of_guest":10,
   "tables":[
      2
   ],
   "reservation_start_at":"{{reservation_start_at}}",
   "reservation_end":"{{reservation_end}}"
}

这篇关于如何为邮递员请求生成未来的日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆