pub struct RequestBuilder<'a, C = Client, B = InMemoryBody> {
pub version: Version,
pub method: Method,
pub uri: Uri,
pub headers: HeaderMap,
pub body: Option<B>,
pub middlewares: Vec<Arc<dyn Middleware>>,
/* private fields */
}Expand description
Provide a custom request builder for several reasons:
- The required reason is have it implement IntoFuture, so that it can be directly awaited.
- The secondary reasons is directly storing client & middlewares on the RequestBuilder. In theory it could be stored on Request.extensions, but that’s less explicit.
- It’s also nice to not require implementing an Extension trait to get all the convenience methods on http::request::RequestBuilder
Middlewares are used in order (first to last).
Fields§
§version: Version§method: Method§uri: Uri§headers: HeaderMap§body: Option<B>§middlewares: Vec<Arc<dyn Middleware>>Implementations§
Source§impl<'a> RequestBuilder<'a, ()>
impl<'a> RequestBuilder<'a, ()>
pub fn get(url: &str) -> RequestBuilder<'a, ()>
pub fn post(url: &str) -> RequestBuilder<'a, ()>
pub fn put(url: &str) -> RequestBuilder<'a, ()>
pub fn delete(url: &str) -> RequestBuilder<'a, ()>
pub fn head(url: &str) -> RequestBuilder<'a, ()>
Source§impl<'a, C> RequestBuilder<'a, C>
impl<'a, C> RequestBuilder<'a, C>
pub fn new( client: &'a C, method: Method, uri: Uri, ) -> RequestBuilder<'a, C, InMemoryBody>
pub fn form<S: Serialize>(self, obj: S) -> Self
Sourcepub fn set_json<S: Serialize>(self, obj: S) -> Self
pub fn set_json<S: Serialize>(self, obj: S) -> Self
Overwrite the current body with the provided JSON object.
Sourcepub fn json<S: Serialize>(self, obj: S) -> Self
pub fn json<S: Serialize>(self, obj: S) -> Self
Add the provided JSON object to the current body.
Sourcepub fn bytes(self, bytes: Vec<u8>) -> Self
pub fn bytes(self, bytes: Vec<u8>) -> Self
Sets content-type to application/octet-stream and the body to the supplied bytes.
Sourcepub fn text(self, text: String) -> Self
pub fn text(self, text: String) -> Self
Sets content-type to text/plain and the body to the supplied text.
pub fn multipart<B>(self, form: Form<B>) -> Self
Source§impl<'a> RequestBuilder<'a>
impl<'a> RequestBuilder<'a>
Sourcepub async fn send(self) -> ProtocolResult<Response>
pub async fn send(self) -> ProtocolResult<Response>
There are two ways to trigger the request. Immediately using .await will call the IntoFuture implementation
which also awaits the body. If you want to await them separately, use this method .send()
Source§impl<'a, C, B: Default> RequestBuilder<'a, C, B>
impl<'a, C, B: Default> RequestBuilder<'a, C, B>
pub fn build(self) -> Request<B>
pub fn into_req_and_middleware(self) -> (Request<B>, Vec<Arc<dyn Middleware>>)
Source§impl<'a, C, B> RequestBuilder<'a, C, B>
impl<'a, C, B> RequestBuilder<'a, C, B>
pub fn for_client(client: &'a C) -> RequestBuilder<'a, C>
pub fn method(self, method: Method) -> Self
pub fn url(self, uri: &str) -> Self
pub fn set_headers<S: AsRef<str>, I: Iterator<Item = (S, S)>>( self, headers: I, ) -> Self
pub fn headers<S: AsRef<str>, I: Iterator<Item = (S, S)>>( self, headers: I, ) -> Self
pub fn header<K: TryInto<HeaderName>>(self, key: K, value: &str) -> Self
pub fn bearer_auth(self, token: &str) -> Self
pub fn token_auth(self, token: &str) -> Self
pub fn basic_auth(self, token: &str) -> Self
Sourcepub fn set_query<S: Serialize>(self, obj: S) -> Self
pub fn set_query<S: Serialize>(self, obj: S) -> Self
Overwrite the query with the provided value.
Sourcepub fn query(self, k: &str, v: &str) -> Self
pub fn query(self, k: &str, v: &str) -> Self
Add a url query parameter, but keep existing parameters.
§Examples
use httpclient::{Client, RequestBuilder, Method};
let client = Client::new();
let mut r = RequestBuilder::new(&client, Method::GET, "http://example.com/foo?a=1".parse().unwrap());
r = r.query("b", "2");
assert_eq!(r.uri.to_string(), "http://example.com/foo?a=1&b=2");pub fn content_type(self, content_type: &str) -> Self
pub fn set_middlewares(self, middlewares: Vec<Arc<dyn Middleware>>) -> Self
pub fn middleware(self, middleware: Arc<dyn Middleware>) -> Self
Trait Implementations§
Source§impl<'a> IntoFuture for RequestBuilder<'a, Client>
impl<'a> IntoFuture for RequestBuilder<'a, Client>
Source§type Output = Result<Response<InMemoryBody>, Error<Response<InMemoryBody>>>
type Output = Result<Response<InMemoryBody>, Error<Response<InMemoryBody>>>
The output that the future will produce on completion.
Source§type IntoFuture = Pin<Box<dyn Future<Output = <RequestBuilder<'a> as IntoFuture>::Output> + Send + 'a>>
type IntoFuture = Pin<Box<dyn Future<Output = <RequestBuilder<'a> as IntoFuture>::Output> + Send + 'a>>
Which kind of future are we turning this into?
Source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Creates a future from a value. Read more
Auto Trait Implementations§
impl<'a, C = Client, B = InMemoryBody> !Freeze for RequestBuilder<'a, C, B>
impl<'a, C = Client, B = InMemoryBody> !RefUnwindSafe for RequestBuilder<'a, C, B>
impl<'a, C, B> Send for RequestBuilder<'a, C, B>
impl<'a, C, B> Sync for RequestBuilder<'a, C, B>
impl<'a, C, B> Unpin for RequestBuilder<'a, C, B>where
B: Unpin,
impl<'a, C = Client, B = InMemoryBody> !UnwindSafe for RequestBuilder<'a, C, B>
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