Pre request script to update a date of birth

Hi!

I’m quite new to postman and wondering if theres any way to streamline my API testing, although its quite a niche use case, its frustrating to do, and im certain there must be a better way!

I was wondering if theres a function possible to update a DOB in a request? Scenario being, i have a test database connected, and when I want a valid response I have to manually “age” that DOB in line with when it was added to the test DB.

For example
Actual DOB (J2): 06/09/1980
Date of entry on DB (I2): 24/03/2017 (this is the date for the entire DB currently, so is persistent)
Aged DOB: 09/08/1988 (this is worked out on excel currently “= TODAY() - I2 + J2”)

I have to update a CSV with the aged DOB each time I run the collection, is it possible to do this automatically?

Thanks in advance!

Hi @jackdunn93. Welcome to the Postman Community :postman:.

Postman cannot directly connect to or mutate data in your database. Postman can only interact with the interfaces your API exposes to perform certain actions or UPDATE data. This means you will need to expose an endpoint that lets you update the dob and call that endpoint in Postman.

You mentioned having to update a CSV manually after running the collection. Is there any direct correlation between your database, your collection, and the CSV?

I’m not trying to update the database - I am trying to manipulate the input request to get a valid response from the DB by inputting the DOB the DB is expecting.

I update the DOB on the input CSV each time i run it, i was wondering is it possible to have a script that performs that for me, so i can just just the same CSV each time, without having to manually amend the data before running - I’m trying to create a variable date that is calculated from 2 pre defined dates (Todays Date - (Date of DB entry + Date Of Birth)) = {Aged Date Of Birth}

“Date of DB entry” is static content 24/03/2017

for example: Actual DOB 06/09/1980 - Aged DOB (required input for a correct response from my test DB) = 09/08/1988

Derived from “Todays date 24/02/2025 - Entry on DB 24/03/2017 + Actual DOB 06/09/1980” = Aged DOB 08/08/1988

You will need to use a pre-request script.

Postman uses JavaScript under the hood, so you can retrieve the date from the iteration data, then update it as you see fit before saving it as a collection variable with your updated value to be used in the current request.

Postman has the “moment” library that you can include in your scripts that you can manipulate and format the dates.

Try searching the forum for “moment” to see formatting examples.

1 Like

Hey @jackdunn93, you can definitely do what you’re trying to do in a prerequest script. It uses JavaScript, like @michaelderekjones said, but let me give you an example.

I don’t know what the rows are called in your CSV, so I’ll assume that the actual DOB is called actualDOB. If it’s not that, you’ll need to update the script below with the appropriate variable name.

const dbEntryDate = new Date('2017-03-24'); 
const actualDOB = new Date(pm.iterationData.get("actualDOB")); 
const today = new Date();

const agedDOB = new Date(actualDOB.getTime() + (today.getTime() - dbEntryDate.getTime()));

pm.variables.set("agedDOB", agedDOB.toISOString().split('T')[0];

This uses that hardcoded date you mentioned and does the same math from your Excel doc. It then saves the updated date to a variable scoped to this specific request called agedDOB.

Now in the request body, you can reference that variable with {{agedDOB}} instead of whatever you’re using currently. Then you’re good to go!

NOTE - this works as written when iterating over a CSV (note the pm.iterationData.get). If you try to run it on its own you’ll get funny dates.

Let me know if you have any questions!

2 Likes