pub struct RequestBuilder<Event, ExpectBody = Vec<u8>> { /* private fields */ }Expand description
Request Builder
Provides an ergonomic way to chain the creation of a request.
This is generally accessed as the return value from Http::{method}().
§Examples
use crux_http::http::{mime::HTML};
let cmd = Http::post("https://httpbin.org/post")
.body("<html>hi</html>")
.header("custom-header", "value")
.content_type(HTML)
.build()
.then_send(Event::ReceiveResponse);Implementations§
Source§impl<Event, ExpectBody> RequestBuilder<Event, ExpectBody>where
Event: 'static,
ExpectBody: 'static,
impl<Event, ExpectBody> RequestBuilder<Event, ExpectBody>where
Event: 'static,
ExpectBody: 'static,
Sourcepub fn header(
self,
key: impl Into<HeaderName>,
value: impl ToHeaderValues,
) -> Self
pub fn header( self, key: impl Into<HeaderName>, value: impl ToHeaderValues, ) -> Self
Sourcepub fn content_type(self, content_type: impl Into<Mime>) -> Self
pub fn content_type(self, content_type: impl Into<Mime>) -> Self
Sourcepub fn body(self, body: impl Into<Body>) -> Self
pub fn body(self, body: impl Into<Body>) -> Self
Sets the body of the request from any type with implements Into<Body>, for example, any type with is AsyncRead.
§Mime
The encoding is set to application/octet-stream.
§Examples
use serde_json::json;
use crux_http::http::mime;
let cmd = Http::post("https://httpbin.org/post")
.body(json!({"any": "Into<Body>"}))
.content_type(mime::HTML)
.build()
.then_send(Event::ReceiveResponse);§Panics
Panics if the RequestBuilder has not been initialized.
Sourcepub fn body_json(self, json: &impl Serialize) -> Result<Self>
pub fn body_json(self, json: &impl Serialize) -> Result<Self>
Pass JSON as the request body.
§Mime
The encoding is set to application/json.
§Errors
This method will return an error if the provided data could not be serialized to JSON.
§Examples
#[derive(Deserialize, Serialize)]
struct Ip {
ip: String
}
let data = &Ip { ip: "129.0.0.1".into() };
let cmd = Http::post("https://httpbin.org/post")
.body_json(data)
.expect("could not serialize body")
.build()
.then_send(Event::ReceiveResponse);Sourcepub fn body_string(self, string: String) -> Self
pub fn body_string(self, string: String) -> Self
Sourcepub fn body_bytes(self, bytes: impl AsRef<[u8]>) -> Self
pub fn body_bytes(self, bytes: impl AsRef<[u8]>) -> Self
Sourcepub fn body_form(self, form: &impl Serialize) -> Result<Self>
pub fn body_form(self, form: &impl Serialize) -> Result<Self>
Pass form data as the request body. The form data needs to be serializable to name-value pairs.
§Mime
The content-type is set to application/x-www-form-urlencoded.
§Errors
An error will be returned if the provided data cannot be serialized to form data.
§Examples
let form_data = HashMap::from([
("name", "Alice"),
("location", "UK"),
]);
let cmd = Http::post("https://httpbin.org/post")
.body_form(&form_data)
.expect("could not serialize body")
.build()
.then_send(Event::ReceiveResponse);Sourcepub fn query(self, query: &impl Serialize) -> Result<Self, HttpError>
pub fn query(self, query: &impl Serialize) -> Result<Self, HttpError>
Set the URL querystring.
§Examples
#[derive(Serialize, Deserialize)]
struct Index {
page: u32
}
let query = Index { page: 2 };
let cmd = Http::post("https://httpbin.org/post")
.query(&query)
.expect("could not serialize query string")
.build()
.then_send(Event::ReceiveResponse);§Panics
Panics if the RequestBuilder has not been initialized.
§Errors
Returns an error if the query string cannot be serialized.
Sourcepub fn middleware(self, middleware: impl Middleware) -> Self
pub fn middleware(self, middleware: impl Middleware) -> Self
Push middleware onto a per-request middleware stack.
Important: Setting per-request middleware incurs extra allocations.
Creating a Client with middleware is recommended.
Client middleware is run before per-request middleware.
See the middleware submodule for more information on middleware.
§Examples
let cmd = Http::get("https://httpbin.org/redirect/2")
.middleware(crux_http::middleware::Redirect::default())
.build()
.then_send(Event::ReceiveResponse);§Panics
Panics if the RequestBuilder has not been initialized.
Sourcepub fn expect_string(self) -> RequestBuilder<Event, String>
pub fn expect_string(self) -> RequestBuilder<Event, String>
Sourcepub fn expect_json<T>(self) -> RequestBuilder<Event, T>where
T: DeserializeOwned + 'static,
pub fn expect_json<T>(self) -> RequestBuilder<Event, T>where
T: DeserializeOwned + 'static,
Decode a T from a JSON response body prior to dispatching it to the apps update
function.
This has no effect when used with the async API.
§Examples
#[derive(Deserialize)]
struct Response {
slideshow: Slideshow
}
#[derive(Deserialize)]
struct Slideshow {
author: String
}
let cmd = Http::post("https://httpbin.org/json")
.expect_json::<Slideshow>()
.build()
.then_send(Event::ReceiveResponse);Sourcepub fn send_async(self) -> BoxFuture<'static, Result<ResponseAsync>>
pub fn send_async(self) -> BoxFuture<'static, Result<ResponseAsync>>
Sends the constructed Request and returns a future that resolves to ResponseAsync.
but does not consume it or convert the body to an expected format.
Note that this is equivalent to calling .into_future() on the RequestBuilder, which
will happen implicitly when calling .await on the builder, which does implement
IntoFuture. Calling .await on the builder is recommended.
Not all code working with futures (such as the join macro) works with IntoFuture (yet?), so this
method is provided as a more discoverable .into_future alias, and may be deprecated later.
Trait Implementations§
Source§impl<Ev> Debug for RequestBuilder<Ev>
impl<Ev> Debug for RequestBuilder<Ev>
Source§impl<T, Eb> IntoFuture for RequestBuilder<T, Eb>
impl<T, Eb> IntoFuture for RequestBuilder<T, Eb>
Source§fn into_future(self) -> Self::IntoFuture
fn into_future(self) -> Self::IntoFuture
Sends the constructed Request and returns a future that resolves to the response