Skinny’s HTTP Client


Skinny Framework has a quite simple and handy HTTP client library. Of course, you can use it with non-Skinny apps.


Setup


libraryDependencies += "org.skinny-framework" %% "skinny-http-client" % "4.0.0"

Simple Usage


Basically, test code will help you to learn how to use.

http-client/src/test/scala/skinny/http/HTTPSpec.scala

import skinny.http._

// GET
val response: Response = HTTP.get("http://example.com/api?foo=bar")

case class Response(
  status: Int,
  headers: Map[String, String],
  headerFields: Map[String, Seq[String]],
  rawCookies: Map[String, String],
  charset: Option[String],
  body: Array[Byte]) {

  def header(name: String): Option[String] 
  def headerField(name: String): Seq[String]
  def asBytes: Array[Byte]
  def textBody: String
  def asString: String 
}

// GET with QueryParams

val response = HTTP.get("http://example.com/", "foo" -> "bar")
val response = HTTP.get(Request("http://example.com/").queryParams("foo" -> "bar"))
val response = HTTP.get(Request("http://example.com/").queryParam("foo" -> "bar").queryParam("bar" -> "baz"))

// GET with charset
val response = HTTP.get("http://example.com/?foo=bar", "UTF-8")

// Async GET

val response: scala.concurrent.Future[Response] = HTTP.asyncGet("http://example.com/?foo=bar")
val response = HTTP.asyncGet("http://example.com/", "foo" -> "bar")
val response = HTTP.asyncGet(Request("http://example.com/").queryParams("foo" -> "bar"))
val response = HTTP.asyncGet("http://example.com/?foo=bar", "UTF-8")

// POST

val response = HTTP.post("http://example.com/", "foo=bar")
val response = HTTP.post("http://example.com/", "foo" -> "bar")
val response = HTTP.postMultipart("http://example.com/", FormData("toResponse", TextInput("bar")))

val file = new java.io.File("http-client/src/test/resources/sample.txt")
val response = HTTP.postMultipart("http://example.com/", FormData("toResponse", FileInput(file, "text/plain")))

val response = HTTP.asyncPost("http://example.com/", "foo=bar")
val response = HTTP.asyncPost("http://example.com/", "foo" -> "bar")
val response = HTTP.asyncPostMultipart("http://example.com/", FormData("toResponse", TextInput("bar")))

// PUT

val response = HTTP.put("http://example.com/", "foo=bar")
val response = HTTP.put("http://example.com/", "foo" -> "bar")
val response = HTTP.asyncPut("http://example.com/", "foo=bar")
val response = HTTP.asyncPut("http://example.com/", "foo" -> "bar")

// DELETE

val response = HTTP.delete("http://example.com/resource")
val response = HTTP.asyncDelete("http://example.com/resource")

// TRACE

val response = HTTP.trace("http://example.com/resource")
val response = HTTP.asyncTrace("http://example.com/resource")

// OPTIONS

val response = HTTP.options("http://example.com/resource")
val response = HTTP.asyncOptions("http://example.com/resource")
If you find a typo or mistake in this page, please report or fix it. How?