Executing an HTTP request in Scala

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

Dispatch seems to be the standard library used for this task, but I cannot find documentation for it. The main site, to which I refer above, explains in detail what is the promise and how to perform asynchronous programming, but does not actually document the API. There is a periodic table - it looks a little scary - but it only seems useful to people who already know what to do, and only need a reminder for the critical syntax.

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

+60
scala scalaz
Jul 30 '12 at 10:11
source share
7 answers

I use the following: https://github.com/scalaj/scalaj-http .

Here is a simple GET request:

import scalaj.http.Http Http("http://foo.com/search").param("q", "monkeys").asString 

and POST example:

 val result = Http("http://example.com/url").postData("""{"id":"12","json":"data"}""") .header("Content-Type", "application/json") .header("Charset", "UTF-8") .option(HttpOptions.readTimeout(10000)).asString 

Scalaj HTTP is available through SBT:

 libraryDependencies += "org.scalaj" % "scalaj-http_2.11" % "2.3.0" 
+99
Jul 30 '12 at 11:20
source

You can use spray-client . The documentation is not enough (it took me a bit of digging to learn how to make GET requests with request parameters ), but this is a great option if you are already using a spray. And the documentation is better than shipping.

We use it in AI2 over dispatch , as the operators are less symbolic, and we already use spray / actors.

 import spray.client.pipelining._ val url = "http://youruri.com/yo" val pipeline: HttpRequest => Future[HttpResponse] = sendReceive // Post with header and parameters val responseFuture1: Future[String] = pipeline(Post(Uri(url) withParams ("param" -> paramValue), yourPostData) map (_.entity.asString) // Post with header val responseFuture2: Future[String] = pipeline(Post(url, yourPostData)) map (_.entity.asString) 
+6
Oct 07 '14 at 18:43
source

I am using submit: http://dispatch.databinder.net/Dispatch.html

They just released a new version (0.9.0) with a complete new api, which I really like. And it is asynchronous.

Example from the project page:

 import dispatch._ val svc = url("http://api.hostip.info/country.php") val country = Http(svc OK as.String) for (c <- country) println(c) 

edit: This can help you https://github.com/dispatch/reboot/blob/master/core/src/main/scala/requests.scala

+5
Jul 30 '12 at 11:33
source

Another option is playafe-plays, which is a WS Framework Framework library laid out as a standalone lib library:

http://blog.devalias.net/post/89810672067/play-framework-seperated-ws-library

I would not offer this as the best option, but worth mentioning.

+3
Jan 13 '15 at 1:32
source

Why not use Apache HttpComponents ? Here are frequently asked questions about applications that cover a wide range of scenarios.

+2
Jul 30 '12 at 10:16
source

If I can make a shameless plugin, I have an API called Bee-Client , which is just a Scala wrapper for Java's HttpUrlConnection.

+1
Jul 31 '12 at 7:37
source

I had to do the same in order to test one endpoint (in the Integration test). So, the following code to get a response from a GET request in Scala. I am using Scala.io.Source to read from the endpoint and ObjectMapper to convert json to object.

 private def buildStockMasterUrl(url:String, stockStatus:Option[String]) = { stockStatus match { case Some(stockStatus) => s"$url?stockStatus=${stockStatus}" case _ => url } } private def fetchBooksMasterData(stockStatus:Option[String]): util.ArrayList[BooksMasterData] = { val url: String = buildBooksMasterUrl("http://localhost:8090/books/rest/catalogue/booksMasterData",stockStatus) val booksMasterJson : String = scala.io.Source.fromURL(url).mkString val mapper = new ObjectMapper() apper.readValue(booksMasterJson,classOf[util.ArrayList[BooksMasterData]]) } case class BooksMasterData(id:String,description: String,category: String) 

And here is my testing method for the same

 test("validate booksMasterData resource") { val booksMasterData = fetchBooksMasterData(Option(null)) booksMasterData.size should be (740) } 
0
Feb 07 '17 at 9:22
source



All Articles