I need a mature HTTP client library that is idiomatic to scala, concise in usage, simple semantics. I looked at the Apache HTTP and the Scala Dispatch and numerous new libraries that promise an idiomatic Scala wrapping. Apache HTTP client sure demands verbosity, while Dispatch was easily confusing.
What is a suitable HTTP client for Scala usage?
I've used Dispatch, Spray Client and the Play WS Client Library...None of them were simply to use or configure. So I created a simpler HTTP Client library which lets you perform all the classic HTTP requests in simple one-liners.
See an example:
... produces ...
The library is called Cirrus and is available via Maven Central
The documentation is available on GitHub
A little late to the party here, but I've been impressed with spray-client.
It's got a nice DSL for building requests, supports both sync and async execution, as well as a variety of (un)marshalling types (JSON, XML, forms). It plays very nicely with Akka, too.
I've recently started using Dispatch, a bit arcane (great general intro, serious lack of detailed scenario/use-case based docs). Dispatch 0.9.1 is a Scala wrapper around Ning's Async Http Client; to fully understand what going on requires introducing one's self to that library. In practice, the only thing I really had to look at was the RequestBuilder - everything else falling nicely into my understanding of HTTP.
I give the 0.9 release a solid thumbs up (so far!) on getting the job done very simply.. once you get past that initial learning curve.
Dispatch's Http "builder" is immutable, and seems to work well in a threaded environment. Though I can't find anything in docs to state that it is thread-safe; general reading of source suggests that it is.
Do be aware that the RequestBuilder's are mutable, and therefore are NOT thread-safe.
Here are some additional links I've found helpful:
I can't find a ScalaDoc link for the 0.9.* release, so I browse the source code for the 0.9.* release;
ScalaDoc for the 0.8 release; a substantially different beast (today) than 0.9.
The "Periodic" Table of operators, also 0.8 related.
The older 0.8 "dispatch-classic" docs helped me understand how they used the url builders, and gave some hints on how things are tied together that did carry forward to 0.9.
I did a comparison of most major HTTP client libraries available
Dispatch, and a few others libraries, are not maintained anymore. The only serious ones currently are spray-client and Play! WS.
spray-client is a bit arcane in its syntax. play-ws is quite easy to use :
(build.sbt)
(basic usage)
sttp is the Scala HTTP library we've all been waiting for!
It has a fluent DSL for forming and executing requests (code samples from their README):
It supports synchronous, asynchronous, and streaming calls via pluggable backends, including Akka-HTTP (formerly Spray) and the venerable AsyncHttpClient (Netty):
It supports
scala.concurrent.Future
,scalaz.concurrent.Task
,monix.eval.Task
, andcats.effect.IO
- all the major Scala IO monad libraries.Plus it has a few additional tricks up its sleeve:
It has case class representations for both requests and responses (although it doesn't go as far as having e.g. strongly typed headers): https://github.com/softwaremill/sttp/blob/master/core/src/main/scala/com/softwaremill/sttp/RequestT.scala https://github.com/softwaremill/sttp/blob/master/core/src/main/scala/com/softwaremill/sttp/Response.scala
It provides a URI string interpolator:
val test = "chrabąszcz majowy" val testUri: Uri = uri"http://httpbin.org/get?bug=$test"
import com.softwaremill.sttp.circe._ val response: Either[io.circe.Error, Response] = sttp .post(uri"...") .body(requestPayload) .response(asJson[Response]) .send()
Finally, it's maintained by the reliable folks at softwaremill and it's got great documentation.