Simple and concise HTTP client library for Scala

46,647

Solution 1

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:

Solution 2

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)

libraryDependencies += "com.typesafe.play" %% "play-ws" % "2.4.3"

(basic usage)

val wsClient = NingWSClient()
wsClient
  .url("http://wwww.something.com")
  .get()
  .map { wsResponse =>
    // read the response
}

Solution 3

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.

Solution 4

Two Six years after originally responding to this post, I would have a different answer.

I've been using akka-http, a collaboration between the spray and akka teams. It's backed by Lightbend, tightly aligned with the akka async environment... it's the right tool for this job.

Solution 5

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):

val request = sttp
  .cookie("session", "*!@#!@!$")
  .body(file) // of type java.io.File
  .put(uri"http://httpbin.org/put")
  .auth.basic("me", "1234")
  .header("Custom-Header", "Custom-Value")
  .response(asByteArray)

It supports synchronous, asynchronous, and streaming calls via pluggable backends, including Akka-HTTP (formerly Spray) and the venerable AsyncHttpClient (Netty):

implicit val sttpHandler = AsyncHttpClientFutureHandler()
val futureFirstResponse: Future[Response[String]] = request.send()

It supports scala.concurrent.Future, scalaz.concurrent.Task, monix.eval.Task, and cats.effect.IO - all the major Scala IO monad libraries.

Plus it has a few additional tricks up its sleeve:

val test = "chrabąszcz majowy" val testUri: Uri = uri"http://httpbin.org/get?bug=$test"

  • It supports encoders/decoders for request bodies/responses e.g. JSON via Circe:

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.

Share:
46,647

Related videos on Youtube

Jesvin Jose
Author by

Jesvin Jose

Tech lead at Marlabs, Kochi. Loves Sqlalchemy, Django, Vue. Admin of many Telegram coding groups. Memorised several passages by Marcus Aurelius.

Updated on July 05, 2022

Comments

  • Jesvin Jose
    Jesvin Jose over 1 year

    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?

    • Travis Brown
      Travis Brown over 11 years
      I'd suggest sticking with Dispatch. Sure, its operatoriness isn't to everyone's taste (it's not to mine), but it's not really as bad as it looks at first, and I'd guess that it's going to stay the most popular Scala option for a while.
    • Kim Stebel
      Kim Stebel over 11 years
      This might be a bit off topic, but I think we should have a fork of dispatch with plain English method names. This would not be hard to do and maintain
    • Daniel C. Sobral
      Daniel C. Sobral over 11 years
      @KimStebel Dispatch 0.9.x can be used with plain English methods alone.
    • Rick-777
      Rick-777 over 11 years
  • Daniel C. Sobral
    Daniel C. Sobral over 11 years
    Note that the periodic table of operators refer to the previous incarnation of Dispatch, and most of that has been cut off from 0.9.x, and probably won't come back.
  • Richard Sitze
    Richard Sitze over 11 years
    Thank you for that clarification. What little I've used has worked nicely: url builder shortcuts, POST, '<<'
  • Daniel C. Sobral
    Daniel C. Sobral over 11 years
    Yes, the request DSL is there -- it's the response that was dumped, aside things like as.String and as.xml.Elem, which are not symbols.
  • Jesvin Jose
    Jesvin Jose over 11 years
    Please help me with my new question stackoverflow.com/questions/12342062/…
  • Freewind
    Freewind almost 11 years
    Why so many people recommend it? The DSL is hard to understand, the document is poor, the examples are few, I can't make a simplest working demo with it.
  • Display Name
    Display Name almost 11 years
    I'm trying to add it as Maven dependency, no luck. Maybe I must add another repository first?
  • George Pligoropoulos
    George Pligoropoulos over 10 years
    spray client unfortunately states that the chunked requests and responses are not supported. So how could you download or upload very large files in a system with limited memory? (i.e. android & scala)
  • Nicolas Rinaudo
    Nicolas Rinaudo almost 10 years
    Out of curiosity, have you been able to use keep-alive with HttpUrlConnection on a per-connection basis, or is there really no other way than the global system property?
  • Rick-777
    Rick-777 almost 10 years
    The Config object has a keepAlive boolean: it controls whether the connections are to be closed or kept-alive on a per-connection basis (bigbeeconsultants.co.uk/docs/bee-client/latest/…).
  • multitask landscape
    multitask landscape over 9 years
    spray-client depends on spray-can, spray-http, spray-httpx, spray-util. Good that there are no external dependencies by why do I need the whole Spray stack. Also I had problems with deploying it to OSGi container.
  • Admin
    Admin about 9 years
    Spray now depends on Akka.
  • Richard Sitze
    Richard Sitze about 9 years
    As the author of this response, I have to point out that it is dated. 2+ years later I'd start with akka-http, which a collaboration between the spray and akka teams: typesafe.com/blog/akka-http-preview. I haven't actually used it yet, but that's where I would start.
  • Waldemar Wosiński
    Waldemar Wosiński almost 9 years
    Sadly, today Client Api documentation is still empty doc.akka.io/docs/akka-stream-and-http-experimental/1.0-M2/sc‌​ala/…
  • Richard Sitze
    Richard Sitze about 8 years
    And now a year later, I can say I've used akka-http quite extensively. It's got it's warts, but overall I'm very happy with it.
  • mvherweg
    mvherweg about 7 years
    Dispatch had a reboot: github.com/dispatch/reboot So it is a viable candidate again. If you can get over the cryptic function 'names', it is a very pleasant library.
  • cdmckay
    cdmckay over 6 years
    Doesn't Play WS depend on Akka?
  • Andrew Norman
    Andrew Norman about 6 years
    here's the official client side doc for akka-http: doc.akka.io/docs/akka-http/10.0.11/scala/http/client-side/…
  • Andrew Norman
    Andrew Norman about 6 years
    the akka-http client is provided as part of the akka-http-core library. the experimental link provided by @WaldemarWosiński was an initial prototype project for what became the official client in akka-http. Because of that you'll want to use the akka-http link and not the earlier link to the orphaned "experimental" project.
  • Jesse Chisholm
    Jesse Chisholm almost 6 years
    This article comparing a few solutions comes from November 2015: implicitdef.com/2015/11/19/…
  • Coder Guy
    Coder Guy over 5 years
    "spray-client is a bit arcane" - I dig arcane syntax personally, but it can result in some IDE glitches. That said I've only ever run into one syntactical glitch with IntelliJ IDEA and spray-can spec testing.
  • Martin Gladdish
    Martin Gladdish over 3 years
    Is this lib still maintained? The link to the source repo is bust and there doesn't seem to be a build for any versions of scala later than 2.11.