Garling 2.1.7 post request does not send json

Good day community,

My Gatling simulation is defined below:

class GatlingSpec extends Simulation {

val user = “markus”
val password = “Cn3%Vkao0xiY3v”

val postWithJSON = “postWithJSON”

def getJSONStr(uuid: String, obj: String, objType: String) =
StringBody(s"""{
“uuid”:"${uuid}",“objectid”:"${obj}",“objecttype”:"${objType}",
“user”:"${user}",“password”:"${password}"
}"""
)

val postWithJSONStr = getJSONStr(java.util.UUID.randomUUID.toString, “MyObject”, “MyObjectID”)

val httpProtocol = http
.baseURL(“https://id-38ih9fiw.google.com:444/MyServer/”)
.disableCaching
.disableClientSharing

val scn = scenario(“oxGatlingTest”).repeat(2) {
exec(
http(session => “Post with JSON”)
.post(postWithJSON)
.body(postWithJSONStr)
.asJSON
)
}

setUp(scn.inject(atOnceUsers(10))).protocols(httpProtocol)

}

My Scala Play app receives request as below:

def postWithJSON = Action { implicit request =>
val jsonRequest = request.body.asJson.get
/* other code */
}

If I run the above scenario, it fails with “failed: but actually found 400” error so no communication happens with the server on which my app is hosted.

If I remove .asJSON from the test, communication happens (and I can successfully send GET requests without any JSON to my app using Gatling) in the following way:
request is of type play.api.mvc.Request, it is not null
request.body is of type play.api.mvc.AnyContent, it is not null
request.body.asJson is of type Option[AAA] but applying get on it afterwards throws a runtime exception: “java.util.NoSuchElementException: None.get”

Could you please tell me what is the right way to create and send JSON to my app? Love your tool, it is super cool.

Best wishes,
Markus

Hi,

400 means there WAS a communication with your server and that it replies that the request was malformed.
It typically means malformed payload or missing headers.

Lower logging level, or use something like Charles, and check the request that’s being sent.