HttpClient

scamper.http.client.HttpClient
See theHttpClient companion object
trait 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.string()

// 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.{ HttpClient, toClientHttpRequest }
import scamper.http.headers.{ toAccept, toAcceptLanguage }
import scamper.http.types.{ stringToMediaRange, stringToLanguageRange }

given HttpClient = HttpClient()
given BodyParser[String] = BodyParser.string(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.

Attributes

Companion
object
Graph
Supertypes
class Object
trait Matchable
class Any

Members list

Value members

Abstract methods

def accept: Seq[MediaRange]

Gets accepted content types.

Gets accepted content types.

Attributes

Gets accepted encodings.

Gets accepted encodings.

Attributes

def bufferSize: Int

Gets buffer size.

Gets buffer size.

Attributes

def continueTimeout: Int

Gets continue timeout.

Gets continue timeout.

Attributes

Gets cookie store.

Gets cookie store.

Attributes

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 parameters

cookies

request cookies

handler

response handler

headers

request headers

target

request target

Attributes

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 parameters

cookies

request cookies

handler

response handler

headers

request headers

target

request target

Attributes

Returns

value from response handler

Throws
java.lang.IllegalArgumentException

if target is not absolute

def keepAlive: Boolean

Tests whether persistent connections are enabled.

Tests whether persistent connections are enabled.

Attributes

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 parameters

body

message body

cookies

request cookies

handler

response handler

headers

request headers

target

request target

Attributes

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 parameters

body

message body

cookies

request cookies

handler

response handler

headers

request headers

target

request target

Attributes

Returns

value from response handler

Throws
java.lang.IllegalArgumentException

if target is not absolute

def readTimeout: Int

Gets read timeout.

Gets read timeout.

Attributes

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 parameters

handler

response handler

request

outgoing request

Attributes

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 parameters

application

WebSocket application

cookies

cookies to include in WebSocket request

headers

additional headers to include in WebSocket request

target

WebSocket target

Attributes

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)