I'm writing a Simple HTTP Client for Scala. This is a simple example of GET request:
using (new SimpleHttpClient()){ client => val content: Option[String] = client.get( url = "http://localhost:8080/" ) content match { case Some(s) => println(s) case None => println("Request Failed.") } }
You can give request parameters to get() method. And it's possible to send POST request to use post() method instead of get() method. The second example shows how to give request parameters with the POST request:
using (new SimpleHttpClient()){ client => val content: Option[String] = client.post( url = "http://localhost:8080/", params = Map("q" -> "Scala"), encode = "UTF-8" ) ... }
In default SimpleHttpClient returns the response body as Option[String]. You can handle the response to give the your own response handler. The response handler has to be a function has a signature (HttpResponse => T).
using (new SimpleHttpClient()){ client => val content: Array[Byte] = client.post( url = "http://localhost:8080/", handler = { r: HttpResponse => r.getStatusLine.getStatusCode match { case HttpStatus.SC_OK => EntityUtils.toByteArray(r.getEntity()) case _ => throw new RuntimeException("Request Failed.") } } ) ... }
SimpleHttpClient also supports http proxy. You can give proxy settings to the constructor.
using(new SimpleHttpClient( "proxy.example.com:8080")){ client => ... }
I'm making useful utilities like this HTTP Client for development of general Scala based applications at scala-utils.