代码之家  ›  专栏  ›  技术社区  ›  Nespony

Akka Http无法读取响应正文?

  •  2
  • Nespony  · 技术社区  · 7 年前

      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 回复  |  直到 7 年前