longcao / requests   0.1.6

MIT License GitHub

Requests for Scala is a small library for making HTTP requests. A port of Requests for Python.

Scala versions: 2.10

Requests for Scala

Build Status Coverage Status

Requests for Scala is a small library for making HTTP requests and built on top of AsyncHttpClient/async-http-client but with more Scala goodies and an API based on the fantastic Requests: HTTP for Humans Python library.

Features

  • Start quickly: a focus on making requests simple
  • Async: all request methods return scala.concurrent.Futures
  • Attempts to implement most of the Requests API where applicable
  • Wishlist:
    • Streaming
    • Sessions
    • Redirect history

Install

Requests for Scala is cross published for 2.10.5 and 2.11.7. Add the Bintray resolver and the dependency to your build.sbt to use it:

resolvers += Resolver.bintrayRepo("longcao", "maven")

libraryDependencies += "org.requests" %% "requests" % "0.1.6"

// java client underneath uses slf4j, add one of these to your deps if you don't want to deal with logging for now
libraryDependencies += "org.slf4j" % "slf4j-nop" % "1.7.12"    // noop
libraryDependencies += "org.slf4j" % "slf4j-simple" % "1.7.12" // all logging goes to console

Quickstart

import org.requests._
import org.requests.status.Status
import org.requests.Implicits._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future

object Main extends App {
  val requests = Requests()
  val response: Future[Response] = requests.get(url = "http://httpbin.org/get")
  val content: Future[String] = response.map(r => new String(r.content))
  val status: Future[Status] = response.map(_.status) // org.requests.status.OK
  
  requests.close // close the underlying AsyncHttpClient when you're done
}

Caveats

This is a personal project of mine for a simpler, boiled down HTTP API without all the cute DSL nuances, and as such there's probably a lot wrong with it. If you need something production-ready (including more robust security settings, SSL, etc.) and generally easy to use, then I recommend you use Play WS standalone, from which I took cues implementing a Scala facade over async-http-client.