Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple and concise HTTP client library for Scala

Tags:

http

scala

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?

like image 750
Jesvin Jose Avatar asked Sep 08 '12 21:09

Jesvin Jose


3 Answers

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.

like image 71
Richard Sitze Avatar answered Nov 09 '22 08:11

Richard Sitze


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
}
like image 45
implicitdef Avatar answered Nov 09 '22 06:11

implicitdef


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.

like image 21
Mark Tye Avatar answered Nov 09 '22 07:11

Mark Tye