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
Value members
Abstract methods
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
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
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
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
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
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