pub struct RequestBuilder { /* private fields */ }
Available on crate feature http only.
Expand description

A wrapper round web_sys::Request: an http request to be used with the fetch API.

Implementations§

source§

impl RequestBuilder

source

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>.

source

pub fn body(self, body: impl Into<JsValue>) -> Result<Request, Error>

Set the body for this request.

source

pub fn cache(self, cache: RequestCache) -> Self

A string indicating how the request will interact with the browser’s HTTP cache.

source

pub fn credentials(self, credentials: RequestCredentials) -> Self

Controls what browsers do with credentials (cookies, HTTP authentication entries, and TLS client certificates).

source

pub fn headers(self, headers: Headers) -> Self

Replace all the headers.

source

pub fn header(self, key: &str, value: &str) -> Self

Sets a header.

source

pub fn query<'a, T, V>(self, params: T) -> Self
where 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
source

pub fn integrity(self, integrity: &str) -> Self

The subresource integrity value of the request (e.g., sha256-BpfBw7ivV8q2jLiT13fxDYAe2tJllusRSZ273h2nFSE=).

source

pub fn json<T: Serialize + ?Sized>(self, value: &T) -> Result<Request, Error>

Available on crate feature json only.

A convenience method to set JSON as request body

Note

This method also sets the Content-Type header to application/json

source

pub fn method(self, method: Method) -> Self

The request method, e.g., GET, POST.

source

pub fn mode(self, mode: RequestMode) -> Self

The mode you want to use for the request.

source

pub fn observe(self, observe: &ObserverCallback) -> Self

Sets the observer callback.

source

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.
source

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.

source

pub fn referrer_policy(self, referrer_policy: ReferrerPolicy) -> Self

Specifies the referrer policy to use for the request.

source

pub fn abort_signal(self, signal: Option<&AbortSignal>) -> Self

Sets the request abort signal.

source

pub async fn send(self) -> Result<Response, Error>

Builds the request and send it to the server, returning the received response.

source

pub fn build(self) -> Result<Request, Error>

Builds the request.

Trait Implementations§

source§

impl Debug for RequestBuilder

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl TryFrom<RequestBuilder> for Request

§

type Error = Error

The type returned in the event of a conversion error.
source§

fn try_from(value: RequestBuilder) -> Result<Self, Self::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.