'Specifying a request body doing a Gatling POST
I'm a fresh newbie to Gatling. I'm trying to send a POST message to an HTTP API using Gatling. I tried the following:
package app.basic
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import scala.concurrent.duration._
class basicPost extends Simulation {
val headers_10 = Map("Content-Type" -> """application/json""")
object Post {
// repeat is a loop resolved at RUNTIME
val post = repeat(50) {
exec(http("Post Data")
.post("/data")
.queryParam("""size""", "10"))
.headers(headers_10)
.body("""{"id1":"0000000000"}""")
.pause(1)
}
}
val httpConf = http.baseURL("http://amazonperf-env.elasticbeanstalk.com")
val users = scenario("Users").exec(Post.post)
setUp(
users.inject(rampUsers(1000) over (10 seconds))
).protocols(httpConf)
}
However, I get this error when compiling: value body is not a member of io.gatling.core.structure.ChainBuilder possible cause: maybe a semicolon is missing before `value body'?
How do I specify the body of the message that I want to send?
Solution 1:[1]
This is old Gatling 1 syntax (Gatling 1 is deprecated and no longer maintained).
Please read the documentation.
In you case, you'd get something like:
.body(StringBody("""{"id1":"0000000000"}"""))
Solution 2:[2]
Moreover, it looks like you closed your exec blog a bit too fast, just after queryParam("""size""", "10").
The closing parenthesis should after .body(...), not after .queryParam(...).
Solution 3:[3]
you could use the method formParam(key: Expression[String], value: Expression[Any]) to post the message to the API.
Solution 4:[4]
Try to send request body as follows
.body(StringBody("""{
"name": "morpheus",
"job": "leader"
} """)).asJson)
Solution 5:[5]
As per current documentation, it is like this:
.body(StringBody("""{ "id1":"0000000000" }""")).asJson
Also remove extra closing bracket at:
.queryParam("""size""", "10"))
Place the closing bracket correctly like below:
.pause(1))
Solution 6:[6]
You can try something like this:
.body(RawFileBody("test-data/your-request-body.json"))
Here 'test-data' is present in 'resources' folder.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stephane Landelle |
| Solution 2 | Pierre DAL-PRA |
| Solution 3 | jmdodo |
| Solution 4 | Damith Chamika |
| Solution 5 | Anurag |
| Solution 6 | horizon7 |
