espresso/request
Types
Represents an HTTP request. Contains most of the pieces of a URI but additionally contains the HTTP method, headers, and body. Also contains the params which are the parsed elements of a router path.
pub type Request(body, assigns, session) {
Request(
method: Method,
headers: List(Header),
body: body,
scheme: Scheme,
host: String,
port: Option(Int),
path: String,
query: Option(String),
params: Params,
assigns: Option(assigns),
session: Session(session),
)
}
Constructors
-
Request( method: Method, headers: List(Header), body: body, scheme: Scheme, host: String, port: Option(Int), path: String, query: Option(String), params: Params, assigns: Option(assigns), session: Session(session), )
Functions
pub fn assign(req: Request(a, b, c), assigns: b) -> Request(
a,
b,
c,
)
Set the assigns of the request, overwriting any existing assigns.
Examples
type Assigns {
Assigns(authorized: Bool)
}
let router =
router.new()
|> router.middleware(fn(next: Service(BitString, Assigns, BitBuilder)) {
fn(req: Request(BitString, Assigns)) {
let auth = request.get_header(req, "authorization")
case auth {
Ok("Basic OnN1cGVyc2VjcmV0") ->
req
|> assign(Assigns(authorized: True))
|> next()
_ -> send(401, "Unauthorized")
}
}
})
|> get(
"/",
fn(_req: Request(BitString, Assigns)) {
// req.assigns here will be Some(Assigns(authorized: True))
// if the basic auth password is "supersecret"
send(202, "Main Route")
}
)
pub fn from_uri(uri: Uri) -> Result(Request(String, a, b), Nil)
Construct a request from a URI.
pub fn get_cookies(req: Request(a, b, c)) -> List(
#(String, String),
)
Fetch the cookies sent in a request.
Note badly formed cookie pairs will be ignored. RFC6265 specifies that invalid cookie names/attributes should be ignored.
pub fn get_header(request: Request(a, b, c), key: String) -> Result(
String,
Nil,
)
Get the value for a given header.
If the request does not have that header then Error(Nil)
is returned.
pub fn get_param(req: Request(a, b, c), name: String) -> Result(
String,
Nil,
)
Utility function to get a router param from the request.
Examples
import espresso/request.{Request}
import gleam/result
fn handler(req: Request(a)) {
let id = req |> request.get_param("id") |> result.unwrap("")
}
pub fn get_query(request: Request(a, b, c)) -> Result(
List(#(String, String)),
Nil,
)
Decode the query of a request.
pub fn load_session(req: Request(a, b, c)) -> Request(a, b, c)
pub fn map(request: Request(a, b, c), transform: fn(a) -> d) -> Request(
d,
b,
c,
)
Update the body of a request using a given function.
pub fn new() -> Request(String, a, b)
A request with commonly used default values. This request can be used as an initial value and then update to create the desired request.
pub fn path_segments(request: Request(a, b, c)) -> List(String)
Return the non-empty segments of a request path.
pub fn prepend_header(request: Request(a, b, c), key: String, value: String) -> Request(
a,
b,
c,
)
Prepend the header with the given value under the given header key.
Similar to set_header
except if the header already exists it prepends
another header with the same key.
pub fn set_body(req: Request(a, b, c), body: d) -> Request(
d,
b,
c,
)
Set the body of the request, overwriting any existing body.
pub fn set_cookie(req: Request(a, b, c), name: String, value: String) -> Request(
a,
b,
c,
)
Send a cookie with a request
Multiple cookies are added to the same cookie header.
pub fn set_header(request: Request(a, b, c), key: String, value: String) -> Request(
a,
b,
c,
)
Set the header with the given value under the given header key.
If already present, it is replaced.
pub fn set_host(req: Request(a, b, c), host: String) -> Request(
a,
b,
c,
)
Set the method of the request.
pub fn set_method(req: Request(a, b, c), method: Method) -> Request(
a,
b,
c,
)
Set the method of the request.
pub fn set_path(req: Request(a, b, c), path: String) -> Request(
a,
b,
c,
)
Set the path of the request.
pub fn set_port(req: Request(a, b, c), port: Int) -> Request(
a,
b,
c,
)
Set the port of the request.
pub fn set_query(req: Request(a, b, c), query: List(
#(String, String),
)) -> Request(a, b, c)
Set the query of the request.
pub fn set_scheme(req: Request(a, b, c), scheme: Scheme) -> Request(
a,
b,
c,
)
Set the scheme (protocol) of the request.
pub fn to(url: String) -> Result(Request(String, a, b), Nil)
Construct a request from a URL string