HttpClient

Defines HTTP client.

A client can be created using the HttpClient companion object.

import scala.language.implicitConversions

import scamper.http.{ BodyParser, stringToUri }
import scamper.http.client.HttpClient

given BodyParser[String] = BodyParser.text()

// Create HttpClient instance
val client = HttpClient()

def getMessageOfTheDay(): Either[Int, String] =
 // Use client instance
 client.get("http://localhost:8080/motd") { res =>
   res.isSuccessful match
     case true  => Right(res.as[String])
     case false => Left(res.statusCode)
 }

And, if a given client is in scope, you can make use of send() on the request itself.

import scala.language.implicitConversions

import scamper.http.{ BodyParser, stringToUri }
import scamper.http.RequestMethod.Registry.Get
import scamper.http.client.{ ClientHttpRequest, HttpClient }
import scamper.http.headers.{ Accept, AcceptLanguage }
import scamper.http.types.{ stringToMediaRange, stringToLanguageRange }

given HttpClient = HttpClient()
given BodyParser[String] = BodyParser.text(4096)

Get("http://localhost:8080/motd")
 .setAccept("text/plain")
 .setAcceptLanguage("en-US; q=0.6", "fr-CA; q=0.4")
 .send(res => println(res.as[String])) // Send request using client

See also ClientSettings for information about configuring the HTTP client before it is created.

Companion
object
class Object
trait Matchable
class Any

Value members

Abstract methods

def accept: Seq[MediaRange]

Gets accepted content types.

Gets accepted content types.

Gets accepted encodings.

Gets accepted encodings.

def bufferSize: Int

Gets buffer size.

Gets buffer size.

Gets continue timeout.

Gets continue timeout.

Gets cookie store.

Gets cookie store.

def delete[T](target: Uri, headers: Seq[Header], cookies: Seq[PlainCookie])(handler: ResponseHandler[T]): T

Sends DELETE request and passes response to handler.

Sends DELETE request and passes response to handler.

Value Params
cookies

request cookies

handler

response handler

headers

request headers

target

request target

Returns

value from response handler

Throws
java.lang.IllegalArgumentException

if target is not absolute

def get[T](target: Uri, headers: Seq[Header], cookies: Seq[PlainCookie])(handler: ResponseHandler[T]): T

Sends GET request and passes response to handler.

Sends GET request and passes response to handler.

Value Params
cookies

request cookies

handler

response handler

headers

request headers

target

request target

Returns

value from response handler

Throws
java.lang.IllegalArgumentException

if target is not absolute

def post[T](target: Uri, headers: Seq[Header], cookies: Seq[PlainCookie], body: Entity)(handler: ResponseHandler[T]): T

Sends POST request and passes response to handler.

Sends POST request and passes response to handler.

Value Params
body

message body

cookies

request cookies

handler

response handler

headers

request headers

target

request target

Returns

value from response handler

Throws
java.lang.IllegalArgumentException

if target is not absolute

def put[T](target: Uri, headers: Seq[Header], cookies: Seq[PlainCookie], body: Entity)(handler: ResponseHandler[T]): T

Sends PUT request and passes response to handler.

Sends PUT request and passes response to handler.

Value Params
body

message body

cookies

request cookies

handler

response handler

headers

request headers

target

request target

Returns

value from response handler

Throws
java.lang.IllegalArgumentException

if target is not absolute

def readTimeout: Int

Gets read timeout.

Gets read timeout.

def send[T](request: HttpRequest)(handler: ResponseHandler[T]): T

Sends request and passes response to supplied handler.

Sends request and passes response to supplied handler.

Value Params
handler

response handler

request

outgoing request

Returns

value from response handler

Throws
java.lang.IllegalArgumentException

if request.target is not absolute

def websocket[T](target: Uri, headers: Seq[Header], cookies: Seq[PlainCookie])(application: WebSocketApplication[T]): T

Connects to WebSocket server at target path and passes established session to supplied application.

Connects to WebSocket server at target path and passes established session to supplied application.

Value Params
application

WebSocket application

cookies

cookies to include in WebSocket request

headers

additional headers to include in WebSocket request

target

WebSocket target

Returns

value from application

Throws
java.lang.IllegalArgumentException

if target is not WebSocket URI (i.e., it must be absolute URI with either "ws" or "wss" scheme)

scamper.http.websocket.WebSocketHandshakeFailure

if WebSocket handshake fails