Need to have dynamic dates in JSON Post request


#1

I need to make a date field dynamic (ie today’s date + 1 month) in my json post calls in Katalon.

For example I have:

“occursOn”: “2019-12-25”

I need that to be dynamic so that I don’t have to go in on that date and change the hardcoded date in roughly 19 different places.

HELP!


#2

Callout to @Brandon_Hein @kazurayam @Marek_Melocik @Mate_Mrse (re Java/Groovy)


#3

@Amanda_Perkins1

Please share an instance of your JSON text; not partial fragment, a whole text.


#4

{
“ApiKey”: not telling,
“Email”: don’t need to know,
“Items”: [
{
“Data”: [
{
“SchemaFieldSlug”: “ktln-schm”,
“Value”: “katalon api”
}
],
“Events”: [
{
“eventSlug”: “katalon-evt”,
“occursOn”: {{date}}, – right here, need current date + 30 in yyyy-MM-dd
“complete”: false
}
]
}
]
}


#5

Do you want the original {{date}} in the JSON text respected? or is it OK to replace it with the current system clock + 30 days?


#6

replace away. I’m just using {{date}} as a placeholder for this example


#7

Make a new Test Case and paste the following lines; then run it.

import java.time.LocalDate
import java.time.format.DateTimeFormatter

import groovy.json.JsonOutput
import groovy.json.JsonSlurper

/**
 * respoinding to
 * https://forum.katalon.com/t/need-to-have-dynamic-dates-in-json-post-request/37142
 * 
 * @author kazurayam
 */

// define a utility function
String fatefulDay() {
	DateTimeFormatter formatter = DateTimeFormatter.ofPattern("YYYY-MM-dd");
	LocalDate theDate = LocalDate.now().plusDays(30)
	return formatter.format(theDate)
}
// just test it
println "${fatefulDay()}"

String source = '''{
"ApiKey": "not telling",
"Email": "don’t need to know",
"Items": [
{
"Data": [
{
"SchemaFieldSlug": "ktln-schm",
"Value": "katalon api"
}
],
"Events": [
{
"eventSlug": "katalon-evt",
"occursOn": "right here, need current date + 30 in yyyy-MM-dd",
"complete": false
}
]
}
]
}
'''
JsonSlurper slurper = new JsonSlurper()
def parsed = slurper.parseText(source)

println ">>> printing the source in a pretty JSON format"
println JsonOutput.prettyPrint(JsonOutput.toJson(parsed))


// edit the object
parsed.Items[0].Events[0].occursOn = fatefulDay()

println ">>> printing the modified object"
print JsonOutput.prettyPrint(JsonOutput.toJson(parsed))

You will see in the Console something like:

            "Events": [
                {
                    "complete": false,
                    "eventSlug": "katalon-evt",
                    "occursOn": "2020-01-04"
                }
            ]

#8

Ok, misunderstanding. I need that date in order to put it into the json for a Post request… my bad.


#9

I think it is your task. You can do it, can’t you?

Or do you want me to tell you how to change your code? If so, you need to share your code on the whole; and the URL of your target Web App needs to be shared public as well so that anyone can reproduce your problem on their side. I suppose you would be reluctant to disclose the detail.


#10

@Amanda_Perkins1

I don’t know much about WS testing in KS (I test mine via the browser). But managing JSON is something I do.

You probably want to investigate jsonSlurper which will allow you to manage the json string as an object. e.g. myjson.myfield, etc.

https://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html

Regarding the date manipulation, see if this post helps:


#11

What I posted is my code. That’s the json POST call that I am using. This is less about katalon and more about, in JSON, how do I have something like {{currentDate + 30}} in order to have a date be dynamic so I don’t have to change it every time the date passes. I’m not a programmer, I’m a QA. I just so happen to be a rather technical QA that writes automation in katalon, and at times I can write in Selenium in Java. Your response was rather pedantic and rude. I am asking for assistance, if at all possible, in determining how to write, in json, something like currentDate+30 so that I can do my job easier.

EDIT: I apologize for my snark. I recognize that text can be taken with whatever mood the reader is in. I am highly frustrated and definitely felt that … never mind how I felt. I did not need to respond in this manner and I apologize.


#12

That might help. I may have to create it as a keyword or global variable or some such in order to pull it in. But, I couldn’t figure out how to do that since I’m basically using the POST of the API stuff of katalon in order to POST json to my site. But, I don’t know what I don’t know and I’m either having an extremely hard time explaining or I’m making it harder than it needs to be.

If I break it down like this:

If I were using POSTMAN and not KATALON, I would be making a POST request. In the body of the POST request I need a date. But I need that date to be dynamic, ie currentDate+30. If I were only using POSTMAN, is there a way to do that?


#13

That’s how I test mine (but in the browser). I also test “net effects” - the changes that should appear in the app once a post has been submitted.

Never used it.

You can parameterize your post in Katalon. I’m just not familiar with it.


#14

That’s how I test mine (but in the browser). I also test “net effects” - the changes that should appear in the app once a post has been submitted.

That’s exactly what I’m trying to do. I hit the post, then I go look in the UI to make sure the post is there. The trouble I run into is that any date in the past won’t show in our UI. So I have to change the dates. I was hoping that there was a way to grab the current date and add 30 so that I didn’t have to go into every single place where I’ve hard coded the date into the post and change it. Bc we all know that that rarely works - you always miss one and bang your head trying to find it.

You can parameterize your post in Katalon. I’m just not familiar with it.

Yeah, I saw that too. I’m not familiar with it either so I’m not certain that it will take on the date the way I want it.

I was hoping that someone had tried this too. My own dev team doesn’t know how to do it and actually they tell me it can’t be done because json can’t do math. So, I may very well be stuck, but I was really hoping…


#15

Callout to @hpulsford @Ibus (re API/WebService)


#16

I think there is no way to write in json something like currentDate+30 as JSON is just a format of text, not a programming language. You need to write some Groovy script to modify the HTTP Body as you want.


#17

@Amanda_Perkins1 build the needed date as a string variable (see already provided advises) and use SimpleTemplateEngine to build your JSON body as a multi-line string variable from a given template:

http://docs.groovy-lang.org/docs/next/html/documentation/template-engines.html


#18

ok, but how does that go into the json request? Like how do I pull it in? I know how to use GlobalVariables in my katalon scripting, but does the WS portion of Katalon allow me to bring in this particular string variable into the http body of the POST request?


#19

Well, I thank everyone for their help and putting up with me trying to do something that’s a bit weird.

I found a workaround that will probably be best for all involved.

I created a test data spreadsheet and loaded into my test. Then, I used that as a variable in the POST request. So, if I need to change the date, I’ll just go into the calls and update what row the variable is calling. Seems the quickest and easiest solution. (I kinda wish I figured this out earlier)

Thanks again for everyone’s help


#20

you can parametrize the request object and pass the body as a string variable from the testcase, you lazy padawan :stuck_out_tongue: