A Scala REST “get content” client function using Apache HttpClient

As quick post here today, if you need a Scala REST client function, the following source code should be able to work for you, or at least be a good starting point. I’ve been using it in several applications today, and the only thing I think it needs is the ability to set a connection timeout and socket timeout, and I share the code for that down below.

So without much introduction, here’s my Scala REST “get content” client function, using the Apache HttpClient library:

/**
 * Returns the text (content) from a REST URL as a String.
 * Returns a blank String if there was a problem; change this to the
 * Some/None/Option pattern?
 * This function will also throw exceptions if there are problems trying
 * to connect to the url.
 */
def getRestContent(url:String): String = {
  val httpClient = new DefaultHttpClient
  val httpResponse = httpClient.execute(new HttpGet(url))
  val entity = httpResponse.getEntity
  var content = ""
  if (entity != null) {
    val inputStream = entity.getContent
    content = io.Source.fromInputStream(inputStream).getLines.mkString
    inputStream.close
  }
  httpClient.getConnectionManager.shutdown
  return content
}

These are the Apache HttpClient imports from the class where I pulled this function from:

import org.apache.http.HttpEntity
import org.apache.http.HttpResponse
import org.apache.http.client.ClientProtocolException
import org.apache.http.client.HttpClient
import org.apache.http.client.methods.HttpGet
import org.apache.http.impl.client.DefaultHttpClient

Potential problems

There are at least three potential problems with this code as it exists right now:

  • I return a blank String when there is a minor problem (something that might happen without throwing an exception).
  • I don't do anything to handle exceptions that can occur, such as the network or server being down.
  • I don't handle socket timeouts.

In my current code, I prefer that this code doesn't handle exceptions, because I want to handle them in the method that calls this function, but other people may prefer that this function handle the exceptions.

The bigger problem for me is that I'm not doing anything to handle a network/socket timeout. A timeout can be handled using the HttpConnectionParams class, and I need to add that code here. I don't have time for it today, but that code should look like this:

// from stackoverflow.com/questions/7229189/timeout-in-defaulthttpclient-class-android
val httpParams = httpClient.getParams
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeOutSec * 1000)
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutSec * 1000)

Here's another example of setting the timeout values on the DefaultHttpClient, and I think I prefer the order of these method calls better:

val httpGet = new HttpGet(url)
val httpParams = httpClient.getParams
HttpConnectionParams.setConnectionTimeout(httpParams, connectionTimeOutSec * 1000)
HttpConnectionParams.setSoTimeout(httpParams, socketTimeoutSec * 1000)
val httpClient = new DefaultHttpClient(httpParams)
val httpResponse = httpClient.execute(httpGet)

I'll try to merge all this code, and update this article in the near future, but until then, if you're looking for a Scala REST client function that uses the Apache HttpClient library, I hope this has been a helpful starting point.