Reputation: 1195
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
Upvotes: 2
Views: 1517
Reputation: 2185
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.
Upvotes: 2