3
在下面的代碼中,我在第一個gatling請求中獲取一個令牌,並將其保存在名爲auth的變量中。但是,當我嘗試在第二個請求中使用它時,它將發送空字符串來代替auth變量。因此,出於某種原因,auth字符串不會被更新,直到它在第二個請求中被使用。任何人都可以提出任何解決方法,以便我可以將一個請求中返回的值用於另一個請求中?如何使用一個gatling請求返回到另一個請求 - Scala
TIA :)
代碼:
val headers_10 = Map("Content-Type" -> "application/x-www-form-urlencoded")
var a= "[email protected]"
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
}
})