0

我正在尝试执行 GET 请求并打印我返回的 JSON 响应。JSON 响应应如下所示。

{
    "request_status": "Success",
    "workflow_step": "End"
}

这是我的代码:

handle(GetStatus) { args: GetStatus.Args =>
    val client: Service[http.Request, http.Response] =
      Http.client.withTlsWithoutValidation.newService("idm-preprod-fe1.twitter.biz:80")
    val request = http.Request(
      http.Method.Get,
      "/plugin/rest/groupmanagement/getStatus/Create Group Request for mygroup (1638800484991)")
    request.host = "idm-preprod-fe1.twitter.biz"
    Future.value(GetStatusResponse(Option(client(request).toString)))
  }

现在,当我的客户端将请求发送到服务器时,我想打印 JSON,但我得到的格式是。有人可以指导我如何实现这一目标。

Promise@1589131174(state=Transforming(WaitQueue(size=1),Promise@1823443423(state=Transforming(WaitQueue(size=1),Promise@859399396(state=Transforming(WaitQueue(size=4),Promise@1441370332(state=Transforming(WaitQueue(size=2),Promise@1459834(state=Transforming(WaitQueue(size=2),Promise@156947070(state=Transforming(WaitQueue(size=1),Promise@1739595981(state=Transforming(WaitQueue(size=1),Promise@273198152(state=Transforming(WaitQueue(size=1),Promise@478329071(state=Transforming(WaitQueue(size=2),Promise@1175786031(state=Transforming(WaitQueue(size=1),Promise@1749285277(state=Transforming(WaitQueue(size=1),Promise@1733124454(state=Transforming(WaitQueue(size=1),Promise@1257379837(state=Transforming(WaitQueue(size=1),Promise@1192050340(state=Transforming(WaitQueue(size=1),Promise@1114225943(state=Transforming(WaitQueue(size=1),Promise@1417620904(state=Transforming(WaitQueue(size=1),Promise@1638767611(state=Interruptible(WaitQueue(size=2),<function1>))))))))))))))))))))))))))))))))))
4

1 回答 1

1

client(request)给你一个Future[http.Response]: 它代表了将来某个时间可用的响应。

您不能toString()直接使用它,因为它是 aFuture并且因为即使它不是 aFuture它也会是 a Response,这是一个完整的 HTTP 响应,而不仅仅是正文。

我建议阅读更多关于如何Future工作以及如何使用它的信息。

在您的情况下,您应该执行以下操作(伪代码,因为我不熟悉 Twitter 的Future):

client(request).map { httpResponse =>
  val responseBody = httpResponse.body
  GetStatusResponse(Option(responseBody))
}
于 2021-12-08T19:38:19.293 回答