如何通过 dispatch 获取响应内容?

how to get the response content with dispatch?

我看了派发教程,很容易找到如何获取header信息(如果状态是200,如果我了解其他帖子)例如;

def main(args: Array[String]){ 
    val svc = url("http://www.google.com")
    val country = Http(svc OK as.String)
    for (c <- country){
        println(c)
    }
}

但是,我找不到如何获取响应内容。如果有人可以帮助我,我将不胜感激。我认为它应该是应用于 svc 的函数。

文档对此进行了解释:

The above defines and initiates a request to the given host where 2xx responses are handled as a string. Since Dispatch is fully asynchronous, country represents a future of the string rather than the string itself.

(强调我的)其中 country 指的是您的示例的请求,您的示例实际上是 returns body.

请注意,您的代码示例明确转换为 String,但您可以获得原始响应 object,如下所示:

val svc = url("http://www.google.com")
val request = Http(svc)
val response = request()
print(s"Status\n  ${response.getStatusCode}\nHeaders:\n  ${response.getHeaders}\nBody:\n  ${response.getResponseBody}")

这将为您提供 HTTP 状态代码、所有响应 headers 和整个响应 body。

See the entire reference for the Response here