package sihl
Install
Dune Dependency
Authors
Maintainers
Sources
sha256=77f0813d75a88edd14b3396e8b848d94c31c28803299b4b1bd4b78b1de4a2e80
sha512=a8907bc35ea14b7c3a7d638979a2a274860202b2de58b84b5621a4908db001ace493d8aa2e5383f4c8b1847efd256938592f63ef75a41521284b3640d3a7442a
doc/sihl.http/Sihl_http/Response/index.html
Module Sihl_http.Response
Source
include module type of Opium_kernel.Response
val env : t -> Opium_kernel__.Hmap0.t
val body : t -> Cohttp_lwt.Body.t
val code : t -> Cohttp.Code.status_code
module Fields : sig ... end
val sexp_of_t : t -> Sexplib0.Sexp.t
val create :
?env:Opium_kernel__.Hmap0.t ->
?body:Cohttp_lwt.Body.t ->
?headers:Cohttp.Header.t ->
?code:Cohttp.Code.status_code ->
unit ->
t
val of_string_body :
?env:Opium_kernel__.Hmap0.t ->
?headers:Cohttp.Header.t ->
?code:Cohttp.Code.status_code ->
string ->
t
val of_stream :
?env:Opium_kernel__.Hmap0.t ->
?headers:Cohttp.Header.t ->
?code:Cohttp.Code.status_code ->
string Lwt_stream.t ->
t
val of_json :
?env:Opium_kernel.Hmap.t ->
?headers:Cohttp.Header.t ->
?code:Cohttp.Code.status_code ->
Yojson.Safe.t ->
t
val of_response_body : (Cohttp.Response.t * Cohttp_lwt.Body.t) -> t
Decoders
to_json
to_json t
parses the body of the response t
as a JSON structure. If the body of the response cannot be parsed as a JSON structure, None
is returned. Use to_json_exn
to raise an exception instead.
Example
let response = Response.of_json (`Assoc [ "Hello", `String "World" ])
let body = Response.to_json response
body
will be:
`Assoc [ "Hello", `String "World" ]
to_json_exn
to_json_exn t
parses the body of the response t
as a JSON structure. If the body of the response cannot be parsed as a JSON structure, an Invalid_argument
exception is raised. Use to_json
to return an option instead.
to_plain_text
to_plain_text t
parses the body of the response t
as a string.
Example
let response = Response.of_plain_text "Hello world!"
let body = Response.to_json response
body
will be:
"Hello world!"
Getters and Setters
status
set_status
set_status status response
returns a copy of response
with the HTTP status set to content_type
.
General Headers
header key t
returns the value of the header with key key
in the response t
. If multiple headers have the key key
, only the value of the first header will be returned. If you want to return all the values if multiple headers are found, you can use headers
.
headers
headers
returns the values of all headers with the key key
in the response t
. If you want to return the value of only the first header with the key key
, you can use header
.
add_header
add_header (key, value) t
adds a header with the key key
and the value value
to the response t
. If a header with the same key is already persent, a new header is appended to the list of headers regardless. If you want to add the header only if an header with the same key could not be found, you can use add_header_unless_exists
. See also add_headers
to add multiple headers.
add_header_or_replace
add_header_or_replace (key, value) t
adds a header with the key key
and the value value
to the response t
. If a header with the same key already exist, its value is replaced by value
. If you want to add the header only if it doesn't already exist, you can use add_header_unless_exists
. See also add_headers_or_replace
to add multiple headers.
add_header_unless_exists
add_header_unless_exists (key, value) t
adds a header with the key key
and the value value
to the response t
if an header with the same key does not already exist. If a header with the same key already exist, the response remains unmodified. If you want to add the header regardless of whether the header is already present, you can use add_header
. See also add_headers_unless_exists
to add multiple headers.
add_headers
add_headers headers response
adds the headers headers
to the response t
. The headers are added regardless of whether a header with the same key is already present. If you want to add the header only if an header with the same key could not be found, you can use add_headers_unless_exists
. See also add_header
to add a single header.
add_headers_or_replace
add_headers_or_replace (key, value) t
adds a headers headers
to the response t
. If a header with the same key already exist, its value is replaced by value
. If you want to add the header only if it doesn't already exist, you can use add_headers_unless_exists
. See also add_header_or_replace
to add a single header.
add_headers_unless_exists
add_headers_unless_exists headers response
adds the headers headers
to the response t
if an header with the same key does not already exist. If a header with the same key already exist, the header is will not be added to the response. If you want to add the header regardless of whether the header is already present, you can use add_headers
. See also add_header_unless_exists
to add a single header.
remove_header
remove_header (key, value) t
removes all the headers with the key key
from the response t
. If no header with the key key
exist, the response remains unmodified.
Specific Headers
content_type
content_type response
returns the value of the header Content-Type
of the response response
.
set_content_type
set_content_type content_type response
returns a copy of response
with the value of the header Content-Type
set to content_type
.
etag
etag response
returns the value of the header ETag
of the response response
.
set_etag
set_etag etag response
returns a copy of response
with the value of the header ETag
set to etag
.
location
location response
returns the value of the header Location
of the response response
.
set_location
set_location location response
returns a copy of response
with the value of the header Location
set to location
.
cache_control
cache_control response
returns the value of the header Cache-Control
of the response response
.
set_cache_control
set_cache_control cache_control response
returns a copy of response
with the value of the header Cache-Control
set to cache_control
.
cookie
cookie ?signed_with key t
returns the value of the cookie with key key
in the Set-Cookie
header of the response t
. If signed_with
is provided, the cookies will be unsigned with the given Signer and only a cookie with a valid signature will be returned. If the response does not contain a valid Set-Cookie
or if no cookie with the key key
exist, None
will be returned.
cookies
cookies ?signed_with t
returns all the value of the cookies in the Set-Cookie
header of the response t
. If signed_with
is provided, the cookies will be unsigned with the given Signer and only the cookies with a valid signature will be returned. If the response does not contain a valid Set-Cookie
, None
will be returned.
add_cookie
val add_cookie :
?sign_with:Cookie.Signer.t ->
?expires:Cookie.expires ->
?scope:Uri.t ->
?same_site:Cookie.same_site ->
?secure:bool ->
?http_only:bool ->
Cookie.value ->
t ->
t
add_cookie ?sign_with ?expires ?scope ?same_site ?secure ?http_only value t
adds a cookie with value value
to the response t
. If a cookie with the same key already exists, its value will be replaced with the new value of value
. If sign_with
is provided, the cookie will be signed with the given Signer.
add_cookie_unless_exists
val add_cookie_unless_exists :
?sign_with:Cookie.Signer.t ->
?expires:Cookie.expires ->
?scope:Uri.t ->
?same_site:Cookie.same_site ->
?secure:bool ->
?http_only:bool ->
Cookie.value ->
t ->
t
add_cookie_unless_exists ?sign_with ?expires ?scope ?same_site ?secure ?http_only value t
adds a cookie with value value
to the response t
. If a cookie with the same key already exists, it will remain untouched. If sign_with
is provided, the cookie will be signed with the given Signer.