从加特林的编码响应中提取数据 [英] Exctract data from encoded response in Gatling
问题描述
我从一个响应中提取数据时遇到问题,我在Fiddler中对其进行了检查,并收到消息,表明该响应已编码: https://i.postimg.cc/G3VfMtxh/2019-10-15-12-31-41-Progress-Telerik-Fiddler-Web-Debugger.png
I have a problem with extracting data from one response, I check it in Fiddler and I get message that this response is encoded: https://i.postimg.cc/G3VfMtxh/2019-10-15-12-31-41-Progress-Telerik-Fiddler-Web-Debugger.png
当我将此内容复制到 https://jsonpath.com/时,我看到了 https://i. postimg.cc/rpYP4wh4/2019-10-14-22-41-19-jsonpath-online-evaluator.png
When I copy this content to https://jsonpath.com/ I see https://i.postimg.cc/rpYP4wh4/2019-10-14-22-41-19-jsonpath-online-evaluator.png
所以我尝试使用正则表达式,所以在这里进行了检查 https://regex101.com/
So I tried to use regex, I checked it here https://regex101.com/ https://i.postimg.cc/DZ6m6RX1/2019-10-14-22-58-57-Online-regex-tester-and-debugger-PHP-PCRE.png
它可以在在线编辑器中使用,但是脚本存在问题.
And it works in online editor, but there is a problem with script.
import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import java.util.Base64
import io.gatling.http.response._
import java.nio.charset.StandardCharsets.UTF_8
class login2 extends Simulation {
val httpProtocol = http
.baseUrl("myapiaddress")
.inferHtmlResources()
.userAgentHeader("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36")
.proxy(Proxy("localhost", 8888).httpsPort(8888))
val headers_0 = Map(
"Accept" -> "application/json, text/plain, */*",
"Origin" -> "mysiteaddress",
"Sec-Fetch-Mode" -> "cors")
val headers_1 = Map(
"Access-Control-Request-Headers" -> "authorization",
"Access-Control-Request-Method" -> "GET",
"Origin" -> "mysiteaddress",
"Sec-Fetch-Mode" -> "cors")
val headers_2 = Map(
"Accept" -> "application/json, text/plain, */*",
"Origin" -> "mysiteaddress",
"Sec-Fetch-Mode" -> "cors",
"authorization" -> "Bearer ${authToken}")
val headers_3 = Map("Sec-Fetch-Mode" -> "no-cors")
val headers_6 = Map(
"Origin" -> "mysiteaddress",
"Sec-Fetch-Mode" -> "cors",
"content-type" -> "application/x-www-form-urlencoded; charset=UTF-8")
val uri1 = "api/signalr"
val scn = scenario("login2")
.exec(http("request_0")
.post("/api/oauth/token")
.headers(headers_0)
.formParam("username", "yelari@malboxe.com")
.formParam("password", "Zaq1@wsx")
.formParam("grant_type", "password")
.check(jsonPath("$..access_token").exists.saveAs("authToken"))
.resources(http("request_1")
.options("/api/account")
.headers(headers_1),
http("request_2")
.get("/api/account")
.headers(headers_2),
http("request_3")
.get("/UploadedFiles/03765fee-ede8-4689-9a4c-13dd2ada18a4.png")
.headers(headers_3),
http("request_4")
.options("/api/conversations/")
.headers(headers_1),
http("request_5")
.options("/api/notifications")
.headers(headers_1),
http("request_6")
.get(uri1 + "/negotiate?clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
//.check(jsonPath("$.ConnectionToken").exists.saveAs("MyConnectionToken"))
//.check(regex("""a-zA-Z0-9=/+{152}""").findAll.saveAs("MyConnectionToken"))
.headers(headers_6),
http("request_7")
.get("/api/notifications")
.headers(headers_2),
http("request_8")
.get("/api/conversations/")
.headers(headers_2),
http("request_9")
.get("/UploadedFiles/5899f868-8473-4dfc-a39c-f13a94c47b3a.jpeg")
.headers(headers_3),
http("request_10")
.get("/UploadedFiles/26ac4d69-8a63-4575-bec4-849d5a5e194a.png")
.headers(headers_3),
http("request_11")
.get(uri1 + "/start?transport=serverSentEvents&clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionToken=${MyConnectionToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
.headers(headers_6)))
val printSesssionVar = scenario("print session var")
.exec{session => println(session("printSesssionVar").as[String])
session}
setUp(scn.inject(atOnceUsers(1))).protocols(httpProtocol)
}
但是在两种情况下我都会出错
But in both cases I get error
> request_11: Failed to build request: No attribute named 'MyCon 1 (50.00%)
nectionToken' is defined
> status.find.in(200,201,202,203,204,205,206,207,208,209,304), f 1 (50.00%)
ound 404
我试图在request_6上提取它,并在request_11上使用它.
I tried to extract it on request_6 and use it on request_11.
是否可以处理此请求?另一方面,我没有问题地提取了承载令牌.
Is it possible to handle this request? On other I extract bearer token without problems.
推荐答案
您在上面已注释了MyConnectionToken
的两个摘录,但我想您已经知道了吗?
You have both of the extractions for MyConnectionToken
commented out above, but I guess you already know that?
通过将.exec { session =>
块放入同一请求链中来尝试并打印出来,即:
Try and print it out by putting the .exec { session =>
block within the same request chain, i.e.:
// ... other code above
http("request_11")
.get(uri1 + "/start?transport=serverSentEvents&clientProtocol=1.5&Authorization=Bearer%20${authToken}&connectionToken=${MyConnectionToken}&connectionData=%5B%7B%22name%22%3A%22livechat%22%7D%5D")
.headers(headers_6)))
.exec{session => println(session("printSesssionVar").as[String])
session}
因为您声明的是val printSesssionVar = scenario
会话中保存的变量,因此不会被保留.
Because you are declaring val printSesssionVar = scenario
the saved variable from the session isn't being carried over.
这篇关于从加特林的编码响应中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!