Scala http operations
You can simply use java.net.URL to send HTTP GET and HTTP POST requests. You can also set HTTP request headers on the HttpURLConnection like this:
val con = url.openConnection.asInstanceOf[HttpURLConnection]
con.setRequestProperty("Header", "Value")
I have written myself a utility class which does exactly this. You can see it here:
https://github.com/gruenewa/gruenewa-misc/blob/master/gruenewa-wsclient/src/main/scala/gruenewa/wsclient/Service.scala
You could try out Dispatch. A little difficult to grasp at first, but after a while I've started to like it. It works on top of HttpClient.
import dispatch.Http
import Http._
// Get
Http(url("http://youruri.com/yo") >>> System.out)
// Get with header
Http(url("http://youruri.com/yo") <:< Map("Accept" -> "application/json") >>> System.out)
// Post
Http(url("http://youruri.com/yo") << yourPostData >|)
While I appreciate the Dispatch library for all it's worth, the syntax still confuses me a bit.
Someone directed me to scalaj-http the other day which seems a little easier
this is my own implementation of a simple Http client including cookies management. Maybe it will be useful for you. But I'm not sure if header modification is directly possible (it may require your own implementation of URLConnection).
import java.io.OutputStreamWriter
import java.net.{URLConnection, URL}
class Http(userAgent: String,
encoding: String,
HttpRequestTimeout: Int = 15000) {
import collection.JavaConversions._
import Implicits.wrapInputStream
import java.net.URLEncoder.encode
var cookies = Map[String, String]()
private def loadCookies(conn: URLConnection) {
for ((name, value) <- cookies) conn.setRequestProperty("Cookie", name + "=" + value)
}
private def saveCookies(conn: URLConnection) {
conn.getHeaderFields.lift("Set-Cookie") match {
case Some(cList) => cList foreach { c =>
val (name,value) = c span { _ != '=' }
cookies += name -> (value drop 1)
}
case None =>
}
}
private def encodePostData(data: Map[String, String]) =
(for ((name, value) <- data) yield encode(name, encoding) + "=" + encode(value, encoding)).mkString("&")
def Get(url: String) = {
val u = new URL(url)
val conn = u.openConnection()
conn.setRequestProperty("User-Agent", userAgent)
conn.setConnectTimeout(HttpRequestTimeout)
loadCookies(conn)
conn.connect
saveCookies(conn)
conn.getInputStream.mkString
}
def Post(url: String, data: Map[String, String]) = {
val u = new URL(url)
val conn = u.openConnection
conn.setRequestProperty("User-Agent", userAgent)
conn.setConnectTimeout(HttpRequestTimeout)
loadCookies(conn)
conn.setDoOutput(true)
conn.connect
val wr = new OutputStreamWriter(conn.getOutputStream())
wr.write(encodePostData(data))
wr.flush
wr.close
saveCookies(conn)
conn.getInputStream.mkString
}
}