Search code examples
apipostman

How to add current datetime in POST API request?


I want the start and end date to be in current datetime. I don't know whether it is possible. I need that because I want to trigger the data everyday using my pipeline.

Please see the image here


Solution

  • You can remove "start" and "end" from the body of the request and then using Postman's Pre-request Script section (next to Body), add the following lines:

    // 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` });