Andrea Andrea - 4 years ago 206
HTTP Question

Doing HTTP request in Scala

I am trying to issue a simple POST request to a webservice which returns some XML in Scala.

It seems that Dispatch is the standard library used for this task, but I cannot find documentation for it. The main site, which I link above, explains at length what is a promise and how to do asynchronous programming, but does not actually document the API. There is a periodic table - which looks a bit scary - but it only seems useful to people who already know what to do and only need a reminder for the cryptic syntax.

It also seems that Scalaz has some facility for HTTP, but I cannot find any documentation for it either.

Answer Source

I use the following:

Here's a simple GET request:

import scalaj.http.Http

Http("").param("q", "monkeys").asString

and an example of a POST:

val result = Http("").postData("""{"id":"12","json":"data"}""")
  .header("Content-Type", "application/json")
  .header("Charset", "UTF-8")

Scalaj HTTP is available through SBT:

libraryDependencies += "org.scalaj" % "scalaj-http_2.11" % "2.3.0"
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download