How to input variable parameter inside a string in testscript?

I need to input a parameter inside a string in testscript( eg: {{currentdate}} ), Inorder to verify the date in the response matches the date(variable) that I gave in the request URL
For example:
Response: "Invalid date q should not be ‘somedate’

I need to check whether the {{currentdate}} parameter which inputs present date is same as the ‘somedate’ displayed in the response

I hope my question is clear, thanks in advance

Save the {{currentdate}} in your environment variable or global variable, you can use the same in your request header as well as validation in your response.

Thanks Amit, but what happens is when I use it in script. I will be verifying like this

pm.test(“Check errorMessage”, function () {
var jsonData = pm.response.json();

pm.expect(jsonData.errorMessage).to.eql("Invalid date range, \"date\" cannot be after current date {{currentdate}}");

the current date value is inside a string so I’m not able to use it in {{currentdate}} format

Hey @immanuelsamuel,
You should be able to get the value of the variable current data in the script as pm.variables.get("currentdate");
And if you want to concat it with a string, you could try the following:

pm.expect(jsonData.errorMessage).to.eql(`Invalid date range, \"date\" cannot be after current date ${pm.variables.get("currentdate")}`);


1 Like

Thanks a lot @meenakshi.dhanani, It works flawlessly. I would be requiring more help, I just started creating testscripts in postman. So I think I would be posting more questions. It would be very helpful If you share your knowledge.
Thanks a lot again.

1 Like