简体   繁体   English

如何使用一个gatling请求返回另一个请求 - Scala

[英]How to use return of one gatling request into another request - Scala

In the following code, I am getting a token in the first Gatling request, saving it in a variable named auth . 在下面的代码中,我在第一个Gatling请求中获取一个令牌,将其保存在名为auth的变量中。 However, when I try to use it in the second request, it is sending empty string in place of auth variable. 但是,当我尝试在第二个请求中使用它时,它发送空字符串代替auth变量。 So for some reason, the auth string is not being updated till the time it is being used in the second request. 因此,出于某种原因,auth字符串直到在第二个请求中使用时才被更新。 Can anyone suggest any workaround so that I can use the value returned in one request into another request? 任何人都可以建议任何解决方法,以便我可以将一个请求中返回的值用于另一个请求吗?

Code: 码:

  val headers_10 = Map("Content-Type" -> "application/x-www-form-urlencoded")
  var a= "qwerty91@gmail.com"
  var auth = ""
  val scn = scenario("Scenario Name") // A scenario is a chain of requests and pauses
    .exec(http("request_1") // Here's an example of a POST request
      .post("/token")
      .headers(headers_10)
      .formParam("email", a)
      .formParam("password", "password")
      .transformResponse { case response if response.isReceived =>
        new ResponseWrapper(response) {
        val a = response.body.string
        auth = "Basic " + Base64.getEncoder.encodeToString((a.substring(10,a.length - 2) + ":" + "junk").getBytes(StandardCharsets.UTF_8))
     }  
     })
     .pause(2)
     .exec(http("request_2")
       .get("/user")
       .header("Authorization",auth)
       .transformResponse { case response if response.isReceived =>
        new ResponseWrapper(response) {
        val a = response.body.string
     }
   })

You should store the value you need in the session. 您应该在会话中存储所需的值。 Something like this will work, although you'll have to tweak the regex and maybe some other details: 这样的东西会起作用,虽然你必须调整正则表达式和其他一些细节:

val headers_10 = Map("Content-Type" -> "application/x-www-form-urlencoded")
  var a= "qwerty91@gmail.com"
  var auth = ""
  val scn = scenario("Scenario Name") // A scenario is a chain of requests and pauses
    .exec(http("request_1") // Here's an example of a POST request
      .post("/token")
      .headers(headers_10)
      .formParam("email", a)
      .formParam("password", "password")
      .check(regex("token: (\\d+)").find.saveAs("auth")))
    .pause(2)
    .exec(http("request_2")
      .get("/user")
      .header("Authorization", "${auth}"))

Here's the documentation on "checks", which you can use to capture values from a response: 这是关于“检查”的文档,您可以使用它来从响应中捕获值:

http://gatling.io/docs/2.2.2/http/http_check.html http://gatling.io/docs/2.2.2/http/http_check.html

Here is the documentation on the gatling EL, which is the easiest way to use session variables (this is the "${auth}" syntax in the last line above): 这是关于gatling EL的文档,这是使用会话变量的最简单方法(这是上面最后一行中的“$ {auth}”语法):

http://gatling.io/docs/2.2.2/session/expression_el.html http://gatling.io/docs/2.2.2/session/expression_el.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Gatling Scala中进行嵌套的请求调用 - How to make nested request call in gatling scala 如何在 scala 的加特林中将变量的值集成到发布请求的正文中 - How to integrate the value of a variable in the body of a post request in gatling with scala 如何在 Scala 3 项目中使用 Gatling - How to use Gatling in a Scala 3 project 如何忽略加特林中失败的请求 - How to ignore the failed request in Gatling 使用在同一用户的后续请求的供稿器内部的一个请求的加特林供稿器内部生成的值 - Use the value generated inside a Gatling Feeder of one request inside the feeder of a consequent request for the same user 在加特林中将请求的响应字符串解析为另一个方法 - Parse the response string of a request into another method in gatling Gatling - Scala:如何重复请求,直到 API 响应中存在某个响应变量? - Gatling - Scala : How to repeat a request until a certain response variable exists in the API response? 在 Gatling / Scala 中循环来自先前请求的多个响应匹配 - Loop over multiple response matches from previous request in Gatling / Scala 创建一个scala函数来定义Gatling HTTP请求链 - Create a scala function to define Gatling HTTP request chains 如何在 Gatling 的 foreach 循环中调用资源请求? - How to call resource request in a foreach loop in Gatling?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM