简体   繁体   English

Gatling2:将响应主体作为字节数组获取

[英]Gatling2: Get response body as byte array

I want to send a request and will receive a byte[] body response. 我想发送一个请求,并将收到一个byte []正文响应。 Based on this byte[] response I want to extract a value (using protobuf) and the reuse this value in another resquest. 基于这个byte []响应,我想提取一个值(使用protobuf),并在另一个请求中重用此值。

After some hours search, I cannot find a possibility to extract the http response body as a byte array: 经过几个小时的搜索,我找不到将http响应正文提取为字节数组的可能性:

   val httpConfig = http
    .baseURL("http://www.whatever.com")

   val request = exec(http("FirstRequest")
    .post("/message")
    .body(new ByteArrayBody((session: Session) => getFirstRequest(session)))
      .check(status.is(200), ???getByteResponse???))

   val response = exec(http("SecondRequest")
    .post("/message")
    .body(new ByteArrayBody((session: Session) => getSecondRequest(session)))
    .check(status.is(200), ???getByteResponse???))

   val scn = scenario("Request").exec(request,response)

  setUp(scn.inject(atOnce(1 user)))
    .protocols(httpConfig)

Alternatively it would also be fine if I could set a value in getFirstRequest, that I can reuse in getSecondRequest: 另外,如果我可以在getFirstRequest中设置一个值,也可以在getSecondRequest中重用,那也很好:

private def getFirstRequest(session: Session): Array[Byte] = {

    ... setting a session attribute ... (long)

    ... some protobuf stuff returning a byte array ...  

  }

  private def getSecondRequest(session: Session): Array[Byte] = {
      var value= session("value").as[Long]

    ... some protobuf stuff using value from session and then returning byte array...
  }

I think you can try something like this : 我认为您可以尝试这样的事情:

.check(status.is(200), bodyBytes.saveAs("responseBody"))

This will save the body of the response in the virtual user's session. 这会将响应的正文保存在虚拟用户的会话中。

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

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