简体   繁体   中英

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?

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"}"""))

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

您可以使用方法 formParam(key: Expression[String], value: Expression[Any]) 将消息发布到 API。

Try to send request body as follows

.body(StringBody("""{
                           "name": "morpheus",
                           "job": "leader"
                       } """)).asJson)

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

You can try something like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM