How to pass in a value to a http body

Hi,

I would like to pass in a date value to .body of an HTTP request.
For example: I would like to pass expDate to myvalue in .body below. I tried ${expDate} but no luck.
Would someone please help? Thanks.

val expDate = today.plusMonths(3)

http("String body").post("my.post.uri")
  .body("""{ "myContent": "myValue" }""").asJSON

If this value is static, why not just concatenate?

"""{ "myContent": """" + expDate + """" }"""

It works ! Thanks so much Stephane!
I also need to concatenate the value with expDate. The value of myValue is comming from a .csv file.
I’m not sure how the “”"" works? Would you please help me understand the rule?
Thanks for your help!

http://www.tutorialspoint.com/scala/scala_quick_guide.htm

Search for triple quotes.

Thanks Stephane!

I also have another issue with the current time not changing. I saw your old post mention that it has to be call in a function, so I tried this:

.body("""{“id”:”${id}”,“name”:"""" + vName + ((session: Session) => (System.currentTimeMillis)) + “”"",“type”:"${type}"}""").asJSON

The value ((session: Session) => (System.currentTimeMillis)) is replacing by instead of the current time stamp.

I then tried:

exec(session => {session.setAttribute(“timestamp”, System.currentTimeMillis.toString)})

and in my body, I used ${timestamp} but I got compilation error “not found: value $”

.body("""{("""{“id”:”${id}”,“name”:"""" + vName + ${timestamp} + “”"",“type”:"${type}"}""").asJSON

Would you please help? Thanks.

No, you can’t combine Gatling EL and custom function (except for Scala developers who know exactly what they’re doing).
For you, the easiest way is probably to store your timestamp in the session beforehand, then only use EL like in your first tentative, except that ${timestamp} has to be INSIDE the String, not here out of the blue.

Assuming vName is a constant:
.body("""{("""{“id”:”${id}”,“name”:"""" + vName + “”""${timestamp},“type”:"${type}"}""").asJSON