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
impl Request
pub fn get(uri: &str) -> Request
pub fn post(uri: &str) -> Request
pub fn put(uri: &str) -> Request
pub fn delete(uri: &str) -> Request
pub fn head(uri: &str) -> Request
pub fn options(uri: &str) -> Request
pub fn new(meth: &str, uri: &str) -> Result<Request, Error>
sourcepub fn json<T: Serialize + ?Sized>(self, json: &T) -> Result<Self, Error>
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
}
sourcepub fn form<T: Serialize + ?Sized>(self, form: &T) -> Result<Self, Error>
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
}
sourcepub fn query<T: Serialize + ?Sized>(self, query: &T) -> Result<Self, Error>
pub fn query<T: Serialize + ?Sized>(self, query: &T) -> Result<Self, Error>
Add query parameter to the request
sourcepub fn body(self, body: impl Into<Body>) -> Result<Self, Error>
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")?;
sourcepub 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>,
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")?;
sourcepub 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>,
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
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Request
impl RefUnwindSafe for Request
impl Send for Request
impl Sync for Request
impl Unpin for Request
impl UnwindSafe for Request
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more