gleam/http/request
Types
A HTTP request.
The body of the request is parameterised. The HTTP server or client you are using will have a particular set of types it supports for the body.
pub type Request(body) {
Request(
method: http.Method,
headers: List(#(String, String)),
body: body,
scheme: http.Scheme,
host: String,
port: option.Option(Int),
path: String,
query: option.Option(String),
)
}
Constructors
-
Request( method: http.Method, headers: List(#(String, String)), body: body, scheme: http.Scheme, host: String, port: option.Option(Int), path: String, query: option.Option(String), )
Arguments
- headers
-
The request headers. The keys must always be lowercase.
Values
pub fn get_cookies(req: Request(a)) -> 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(body),
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.
Header keys are always lowercase in gleam_http
. To use any uppercase
letter is invalid.
pub fn get_query(
request: Request(body),
) -> Result(List(#(String, String)), Nil)
Decode the query of a request.
pub fn map(
request: Request(old_body),
transform: fn(old_body) -> new_body,
) -> Request(new_body)
Update the body of a request using a given function.
pub fn new() -> Request(String)
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(body)) -> List(String)
Return the non-empty segments of a request path.
Examples
> new()
> |> set_path("/one/two/three")
> |> path_segments
["one", "two", "three"]
pub fn prepend_header(
request: Request(body),
key: String,
value: String,
) -> Request(body)
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.
Header keys are always lowercase in gleam_http
. To use any uppercase
letter is invalid.
pub fn remove_cookie(
req: Request(body),
name: String,
) -> Request(body)
Remove a cookie from a request
Remove a cookie from the request. If no cookie is found return the request unchanged. This will not remove the cookie from the client.
pub fn set_body(
req: Request(old_body),
body: new_body,
) -> Request(new_body)
Set the body of the request, overwriting any existing body.
pub fn set_cookie(
req: Request(body),
name: String,
value: String,
) -> Request(body)
Send a cookie with a request
Multiple cookies are added to the same cookie header.
pub fn set_header(
request: Request(body),
key: String,
value: String,
) -> Request(body)
Set the header with the given value under the given header key.
If already present, it is replaced.
Header keys are always lowercase in gleam_http
. To use any uppercase
letter is invalid.
pub fn set_method(
req: Request(body),
method: http.Method,
) -> Request(body)
Set the method of the request.
pub fn set_query(
req: Request(body),
query: List(#(String, String)),
) -> Request(body)
Set the query of the request. Query params will be percent encoded before being added to the Request.
pub fn set_scheme(
req: Request(body),
scheme: http.Scheme,
) -> Request(body)
Set the scheme (protocol) of the request.