Specifying a request body doing a Gatling POST
Asked Answered
L

6

9

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?

Lethargy answered 10/10, 2014 at 1:6 Comment(0)
E
20

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"}"""))
Executory answered 10/10, 2014 at 5:56 Comment(0)
L
1

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))
Lussi answered 21/12, 2021 at 12:4 Comment(0)
S
0

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(...).

Simulacrum answered 10/10, 2014 at 15:39 Comment(0)
D
0

you could use the method formParam(key: Expression[String], value: Expression[Any]) to post the message to the API.

Decillion answered 9/4, 2015 at 9:7 Comment(0)
S
0

Try to send request body as follows

.body(StringBody("""{
                           "name": "morpheus",
                           "job": "leader"
                       } """)).asJson)
Sophronia answered 3/10, 2020 at 19:2 Comment(1)
Welcome to Stack Overflow. Please edit your answer to explain how it answers the question, so that it is useful to users with similar issues. Code-only answers are discouraged on Stack Overflow because they don't explain how it solves the problem.Downandout
M
0

You can try something like this:

.body(RawFileBody("test-data/your-request-body.json"))

Here 'test-data' is present in 'resources' folder.

Mcfall answered 17/3, 2022 at 20:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.