Akka-Http无法读取响应体?

时间:2018-06-05 20:20:20

标签: scala akka

我在scala / akka-http项目中有以下功能:

  def fetchResponse(userIdentifier: String, password: String): Future[HttpResponse] = {
    myResource(userIdentifier, password).map(
      result =>
            if(result.status.intValue() == 201){
              val body = result.headers.collect {
                case c: `Set-Cookie` => c.cookie
              }.find(_.name == "SSO").getOrElse(HttpCookie("SSO", "")).value
              HttpResponse(result.status.intValue(), entity = body)
            }
            else {
             val body =
              result.httpMessage.entity().toString
              println(result)
              HttpResponse(result.status.intValue(),entity = body)
            }
    )

  }

此方法通过以下函数调用myMethod api:

 def myResource(userIdentifier:String,password:String): Future[HttpResponse] = {
    Http().singleRequest(HttpRequest(uri = Uri("http://localhost:9090/information"),
      method = HttpMethods.POST,
      entity = {
       FormData(Map("userIdentifier" -> userIdentifier, "password" -> password)).toEntity(HttpCharsets.`UTF-8`)
      }
    )
      .withHeaders(
        RawHeader("Content-Type", "application/x-www-form-urlencoded"),
        RawHeader("Accept", "application/vnd.siren+json")
      ))

  }

然而,当我从第一个函数中调用第二个函数时,它只返回响应状态,而不是返回的身体:

HttpEntity.Chunked(application/octet-stream)

出现以下警告:

(WaitingForResponseEntitySubscription)] Response entity was not subscribed after 1 second. Make sure to read the response entity body or call `discardBytes()` on it.

我不确定是什么问题!

0 个答案:

没有答案