pub struct Request { /* private fields */ }
http
only.Expand description
A wrapper round web_sys::Request
: an http request to be used with the fetch
API.
Implementations§
source§impl Request
impl Request
sourcepub fn new(url: &str) -> Self
pub fn new(url: &str) -> Self
Creates a new request that will be sent to url
.
Uses GET
by default. url
can be a String
, a &str
, or a Cow<'a, str>
.
sourcepub fn cache(self, cache: RequestCache) -> Self
pub fn cache(self, cache: RequestCache) -> Self
A string indicating how the request will interact with the browser’s HTTP cache.
sourcepub fn credentials(self, credentials: RequestCredentials) -> Self
pub fn credentials(self, credentials: RequestCredentials) -> Self
Controls what browsers do with credentials (cookies, HTTP authentication entries, and TLS client certificates).
sourcepub fn query<'a, T, V>(self, params: T) -> Selfwhere
T: IntoIterator<Item = (&'a str, V)>,
V: AsRef<str>,
pub fn query<'a, T, V>(self, params: T) -> Selfwhere
T: IntoIterator<Item = (&'a str, V)>,
V: AsRef<str>,
Append query parameters to the url, given as (name, value)
tuples. Values can be of any
type that implements ToString
.
It is possible to append the same parameters with the same name multiple times, so
.query([("a", "1"), ("a", "2")])
results in the query string a=1&a=2
.
Examples
The query parameters can be passed in various different forms:
use std::collections::HashMap;
use gloo_net::http::Request;
let slice_params = [("key", "value")];
let vec_params = vec![("a", "3"), ("b", "4")];
let mut map_params: HashMap<&'static str, &'static str> = HashMap::new();
map_params.insert("key", "another_value");
let r = Request::get("/search")
.query(slice_params)
.query(vec_params)
.query(map_params);
// Result URL: /search?key=value&a=3&b=4&key=another_value
sourcepub fn integrity(self, integrity: &str) -> Self
pub fn integrity(self, integrity: &str) -> Self
The subresource integrity value of the request (e.g.,
sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=
).
sourcepub fn json<T: Serialize + ?Sized>(self, value: &T) -> Result<Self, Error>
pub fn json<T: Serialize + ?Sized>(self, value: &T) -> Result<Self, Error>
A convenience method to set JSON as request body
Note
This method also sets the Content-Type
header to application/json
sourcepub fn mode(self, mode: RequestMode) -> Self
pub fn mode(self, mode: RequestMode) -> Self
The mode you want to use for the request.
sourcepub fn observe(self, observe: &ObserverCallback) -> Self
pub fn observe(self, observe: &ObserverCallback) -> Self
Sets the observer callback.
sourcepub fn redirect(self, redirect: RequestRedirect) -> Self
pub fn redirect(self, redirect: RequestRedirect) -> Self
How to handle a redirect response:
- follow: Automatically follow redirects. Unless otherwise stated the redirect mode is set to follow
- error: Abort with an error if a redirect occurs.
- manual: Caller intends to process the response in another context. See WHATWG fetch standard for more information.
sourcepub fn referrer(self, referrer: &str) -> Self
pub fn referrer(self, referrer: &str) -> Self
The referrer of the request.
This can be a same-origin URL, about:client
, or an empty string.
sourcepub fn referrer_policy(self, referrer_policy: ReferrerPolicy) -> Self
pub fn referrer_policy(self, referrer_policy: ReferrerPolicy) -> Self
Specifies the referrer policy to use for the request.
sourcepub fn abort_signal(self, signal: Option<&AbortSignal>) -> Self
pub fn abort_signal(self, signal: Option<&AbortSignal>) -> Self
Sets the request abort signal.