简体   繁体   中英

Make dynamic request using Gatling

I want to make a dynamic request using Gatling. using Gatling we are getting to rest-API to which we are posting an SMS request I want that each request should be done with a different sent of mob no.

    exec(http("Sms Delivery")
      .post(s"/deliveries")
      .body(StringBody(
        s"""{ "template":
           |{ "type":"inline", "inline":
           |  { "channels":
           |    "sms":{ "body":{ "layout":"{greeting}", "fragments":{ "greeting":{"en-US":"ABC123 Interested to hear about some jobs?"}}}} }} },
           | "recipients":[ { "contactDetails":{ "mobileNumber" : "+1${Random.nextInt(123123123)}" } }],
           |  "requester": "${UUID.random.toString}"}""".stripMargin)).asJson
      .check(status.is(202))
      .check(jsonPath("$.messageId").ofType[String])
    )
  }

setUp(
    scenario("Create message-Cmd")
      .exec(createMessageServiceSms()).inject(
      rampConcurrentUsers(0) to (6) during(1),
      constantConcurrentUsers(5) during (testTime seconds)
    )
  )

Let suppose Gatling sends 100 requests the for all those requests my mob no remains the same for each request is same let say +12011705515 . However, I want all no. should be different.

The string is created only once, then parsed as Expression Language , which allows you to substitute in session attributes .

If you want custom functions like creating a UUID and calling Random.nextInt , you need to pass in a function to StringBody .

StringBody(_ =>
  s"""{ "template":
     |{ "type":"inline", "inline":
     |  { "channels":
     |    "sms":{ "body":{ "layout":"{greeting}", "fragments":{ "greeting":{"en-US":"ABC123 Interested to hear about some jobs?"}}}} }} },
     | "recipients":[ { "contactDetails":{ "mobileNumber" : "+1${Random.nextInt(123123123)}" } }],
     |  "requester": "${UUID.random.toString}"}""".stripMargin)
)

The parameter that we discarded is the virtual user's Session .


Worth noting that you may have too few digits for the mobileNumber , but that's another issue than creating a dynamic request.

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