如何读取响应标头 +使用 scala 的调度库从单个 POST 获取正文

发布于 2024-09-24 19:02:52 字数 594 浏览 7 评论 0原文

我一直在尝试使用 dispatch 库通过 http POST 请求下载文件。服务器返回一个“content-disposition”标头,建议其返回的数据文件的文件名。

我已成功将整个响应正文作为字符串读取,

http(r >~ { (x) => println(x.getLines.mkString("","\n","")) })

自行读取响应标头

http(r >:> { (x) => println(x) })

并获取响应正文的 BufferedReader

http(r >> { (x,c) => (new BufferedReader(new InputStreamReader(x,c))).readLine })

我将如何使用调度库一次性获取响应标头和响应正文?文档非常稀疏,而且我是 Scala 新手。

蒂亚

·迈克尔

I have been trying to use the dispatch library to download a file via an http POST request. The server returns a "content-disposition" header suggesting a filename for the data file it returns.

I have succeeded reading the entire response body as a string,

http(r >~ { (x) => println(x.getLines.mkString("","\n","")) })

reading the response headers on their own

http(r >:> { (x) => println(x) })

and getting a BufferedReader for the response body

http(r >> { (x,c) => (new BufferedReader(new InputStreamReader(x,c))).readLine })

How would I go about getting the response headers AND the response body in one go using the dispatch lib? The docs are very sparse and I am new to Scala.

TIA

Michael

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如梦 2024-10-01 19:02:52

Dispatch 使用 Handler 来处理来自请求的 HTTP 响应。它提供了一些用于执行例行任务的便捷快捷方式,例如您在问题中概述的快捷方式,例如生成 InputStream、以字符串形式返回内容或查看响应的标头。还有一个方法, >+,它组成两个独立的 Handler 并在同一个请求上执行它们。以下是使用该处理程序解决问题的方法:

val ret = http(req >+ { r => (r as_str, r >:> { _("Content-Disposition") }) })

返回值是一个 Tuple2,在本例中,它包含一个字符串(网页内容)和另一个字符串(Content-Disposition 标头的值)。

Dispatch uses Handlers to handle HTTP responses from a request. It provides several handy shortcuts for performing routine tasks, like the ones that you outlined in the question such as generating an InputStream, returning the content as a string, or looking at the headers of the response. There is also a method, >+, which composes two separate Handlers and executes them on the same request. Here's how you could solve your problem using that handler:

val ret = http(req >+ { r => (r as_str, r >:> { _("Content-Disposition") }) })

The return value is a Tuple2 which contains, in this case, a string which is the content of the web page and another string which is the value of the Content-Disposition header.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文