This is a bare bones http client for scala which wraps HttpURLConnection
import scalaj.http.Http
Http("http://foo.com/search").param("q","monkeys").asStringHttp.post("http://foo.com/add").params("name" -> "jon", "age" -> "29").asStringimport scalaj.http.{Http, Token}
val consumer = Token("key", "secret")
val token = Http("http://foursquare.com/oauth/request_token").param("oauth_callback","oob")
.oauth(consumer).asToken
println("Go to http://foursquare.com/oauth/authorize?oauth_token=" + token.key)
val verifier = Console.readLine("Enter verifier: ").trim
val accessToken = Http("http://foursquare.com/oauth/access_token")
.oauth(consumer, token, verifier).asToken
println(Http("http://api.foursquare.com/v1/history.json").oauth(consumer, accessToken).asString)Http("http://foo.com").{responseCode, asString, asXml, asBytes, asParams}val scalaj_http = "org.scalaj" %% "scalaj-http" % "0.3.10"<dependency>
<groupId>org.scalaj</groupId>
<artifactId>scalaj-http_${scala.version}</artifactId>
<version>0.3.10</version>
</dependency> import java.io.InputStreamReader
import net.liftweb.json.JsonParser
Http("http://foo.com"){inputStream =>
JsonParser.parse(new InputStreamReader(inputStream))
}Http.postData(url, data).header("content-type", "application/json").responseCodeHttp.multipart(url, MultiPart("photo", "headshot.png", "image/png", fileBytes)).responseCodeYou can also stream uploads and get a callback on progress:
Http.multipart(url, MultiPart("photo", "headshot.png", "image/png", inputStream, bytesInStream,
lenWritten => {
println("Wrote %d bytes out of %d total for headshot.png".format(lenWritten, bytesInStream))
})).responseCodeHttp("https://localhost/").option(HttpOptions.allowUnsafeSSL).asStringHttp(url).option(HttpOptions.method("HEAD")).asStringThese are set to 100 and 500 milliseconds respectively by default
Http(url).option(HttpOptions.connTimeout(1000)).option(HttpOptions.readTimeout(5000)).asStringval (responseCode, headersMap, resultString) = Http(url).asHeadersAndParse(Http.readString)val response = Http(url).proxy(proxyHost, proxyPort).asStringThe .option() method takes a function of type HttpURLConnection => Unit so
you can manipulate the connection in whatever way you want before the request executes.
By default, the charset for all param encoding and string response parsing is UTF-8. You can override with charset of your choice:
Http(url).charset("ISO-8859-1").asString- Handle other http methods better (PUT,HEAD,DELETE)
