Struct generic_async_http_client::Request

source ·
pub struct Request(/* private fields */);
Expand description

Builds a HTTP request, poll it to query

    let req = Request::get("http://example.com/");
    let resp = req.exec().await?;

Depending on the chosen implementation, Request implements TryFrom<(TryInto<Method>, TryInto<Url>)>.

Implementations§

source§

impl Request

source

pub fn get(uri: &str) -> Request

source

pub fn post(uri: &str) -> Request

source

pub fn put(uri: &str) -> Request

source

pub fn delete(uri: &str) -> Request

source

pub fn head(uri: &str) -> Request

source

pub fn options(uri: &str) -> Request

source

pub fn new(meth: &str, uri: &str) -> Result<Request, Error>

source

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

Add a JSON body to the request

#[derive(Serialize)]
struct JoseBody {
    protected: String,
    payload: String,
    signature: String,
}
async fn jose(jose: &JoseBody) -> Result<Response, Error> {
   let req = Request::put("http://example.com/").json(jose)?;
   req.exec().await
}
source

pub fn form<T: Serialize + ?Sized>(self, form: &T) -> Result<Self, Error>

Add a form data body to the request

#[derive(Serialize)]
struct ContactForm {
    email: String,
    text: String,
}
async fn post_form(form: &ContactForm) -> Result<Response, Error> {
   let req = Request::post("http://example.com/").form(form)?;
   req.exec().await
}
source

pub fn query<T: Serialize + ?Sized>(self, query: &T) -> Result<Self, Error>

Add query parameter to the request

source

pub fn body(self, body: impl Into<Body>) -> Result<Self, Error>

Add a body to the request

    let req = Request::post("http://example.com/").body("some body")?;
source

pub fn set_header<N, V, E1, E2>(self, name: N, value: V) -> Result<Self, Error>
where N: TryInto<HeaderName, Error = E1>, V: TryInto<HeaderValue, Error = E2>, Error: From<E1> + From<E2>,

Add a single header to the request If the map did have this key present, the new value is associated with the key

    let req = Request::get("http://example.com/").set_header("User-Agent", "generic_async_http_client v0.2")?;
source

pub fn add_header<N, V, E1, E2>(self, name: N, value: V) -> Result<Self, Error>
where N: TryInto<HeaderName, Error = E1>, V: TryInto<HeaderValue, Error = E2>, Error: From<E1> + From<E2>,

Add a single header to the request If the map did have this key present, the new value is pushed to the end of the list of values

source

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

Send the request to the webserver

Trait Implementations§

source§

impl Debug for Request

source§

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

Formats the value using the given formatter. Read more

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.