Invalid Date time format value error

Hi,
I am trying to inject date variable to my json request body but getting this error:

body:
[{"errors":[{"message":"Variable $limitingPeriodEnd of type ISO8601DateTime was provided invalid value","locations":[{"line":1,"column":203}],"extensions":{"value":"#{endOfWeekDate}","problems":[{"path":[],"explanation":"Could not coerce value \"#{endOfWeekDate}\" to ISO8601DateTime"}]}},{"message":"Variable $limitingPeriodStart of type ISO8601DateTime was provided invalid value","locations":[{"line":1,"column":240}],"extensions":{"value":"#{todaysDate}","problems":[{"path":[],"explanation":"Could not coerce value \"#{todaysDate}\" to ISO8601DateTime"}]}},{"message":"Variable $now of type ISO8601DateTime was provided invalid value","locations"

Here is the session information with the generated date values:
Session:
Session(Booking manager Navigation Booked,2,HashMap(gatling.http.cache.wsBaseUrl -> ... gatling.http.cache.baseUrl -> ., todaysDate -> 2022-11..-02T14:04:42.607Z, endOfWeekDate -> 2022-11-08T15:04:42.607Z

Here how I inject them:

val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
  dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"))
  val timeNow = (Calendar.getInstance())

val scnBookingManagerBooked = scenario("Booking manager Navigation Booked")
    .exec(session => {
      session.set(
        "todaysDate",
        dateFormat.format(timeNow.getTime())
      )
    })
    .exec(session => {
      timeNow.add(Calendar.DAY_OF_WEEK, +6)
      session.set(
        "endOfWeekDate",
        dateFormat.format(timeNow.getTime())
      )
    })

Finally here is the .json file I use as the request body for the graphql call:

{
        "operationName": "Bookings",
        "variables": {
            "bookingType": null,
            "first": 15,
            "limitBookings": true,
            "limitingPeriodEnd": "#{endOfWeekDate}",
            "limitingPeriodStart": "#{todaysDate}",
            "now": "#{todaysDate}",
            "safeguardCheck": null,
.....

I appreciate if you can help me figure out why I am getting the error even the format is correct.
I tried changing the data.format to String but that didn’t work.
One last thing. I don’t get an error when I copy and paste the date format from the session (logs of previously ran test), into the json file manually and run the test. So the format is the correct format.

Thanks,

First, SimpleDateFormat is not thread-safe. What you’re doing here is share a single instance and call format from multiple treads. This is going to cause erroneous results under load. You should use the java.time API which is thread-safe.

Then, note that timeNow is a constant, not something that’s evaluated in every function. As is, all the virtual users will have the same value.

Finally, it looks like your #{} in your request body template are not being evaluated.
How are you passing it? StringBody?
Are you using Gatling 3.8 where the #{} syntax was introduced?

Thank you!
I am switch to java.time API but not I don’t see the values in the session log. Since I switched to java.time, I didn’t need to use the ‘timeNow’ constant.

I use Gatling 3.8.4. I am passing it as a string into my json request body file.

Here is how I tried to create the dateTime and pass it:

val f = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
val d = LocalDateTime.now
val scnBookingManagerBooked = scenario("Booking manager Navigation Booked")
    .exec(session => {
      session.set(
        "todaysDate",
        f.format(d).toString()
      )
    })
    .exec(session => {
      d.plusDays(6)
      session.set(
        "endOfWeekDate",
        f.format(d).toString()
      )
    })
    .exec(HuddleCookie.injectIntoSession)
    .exec(navigateTo("/booking-manager"))
    .exec(BookingManagerRequests.bookedBookings) //this call uses the #{} values in json

Here is the request that uses the json values

val bookedBookings = exec(
    http(getGraphQLName("bookingManager/bookedBookings.json"))
      .post("/graphql")
      .headers(graphqlHeader)
      .body(
        RawFileBody("bookingManager/bookedBookings.json")
      )
      .check(noGraphQLErrors)
      .check(jsonPath("$[0].data.bookings.totalCount").ofType[Int].is(4))
  )```

DateTime is immutable.
d.plusDays(6) returns a new instance, doesn’t mutate d.

RawFileBody means RAW = no interpolation. You want to use ElFileBody to enable Gatling EL (Expression Language).

1 Like

Thank you so much! I knew I was making a stupid mistake! I fixed it and it is working now.
I appreciate your help Stephane.