HTTP client examples in Scala
When learning Scala I found very few good examples of HTTP clients in use. This project contains a set of example HTTP clients that use a range of popular Scala and Java libraries.
All of the example clients support:
- Request headers and query string paramaters passed in as
Map
's GET
,POST
,PUT
andDELETE
operations
The same interface is implemented by all of the example clients:
trait HttpClient {
def get(url: String, params: Map[String, String], headers: Map[String, String]): Response
def put(url: String, body: String, params: Map[String, String], headers: Map[String, String]): Response
def post(url: String, body: String, params: Map[String, String], headers: Map[String, String]): Response
def delete(url: String, params: Map[String, String], headers: Map[String, String]): Response
}
where the Response
object is a case class:
case class Response(statusCode: Int, body: String, headers: Map[String, String])
An asynchronous version of the interface is implemented where asyncronous clients are used. The asynchronous return type is Future[Response]
.
Client | Library | Sync/Async |
---|---|---|
DispatchHttpClient | Dispatch | Async |
ApacheHttpClient | Apache HttpComponents Client | Sync |
SprayHttpClient | Sprary client | Async |