pub struct RequestBuilder<Effect, 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
crux_http::command::Http::{method}()
.
§Examples
Http::post("https://httpbin.org/post")
.body("<html>hi</html>")
.header("custom-header", "value")
.content_type(crux_http::http::mime::HTML)
.build()
.then_send(Event::ReceiveResponse);
Implementations§
Source§impl<Effect, Event, ExpectBody> RequestBuilder<Effect, Event, ExpectBody>where
Effect: Send + From<Request<HttpRequest>> + 'static,
Event: Send + 'static,
ExpectBody: 'static,
impl<Effect, Event, ExpectBody> RequestBuilder<Effect, Event, ExpectBody>where
Effect: Send + From<Request<HttpRequest>> + 'static,
Event: Send + '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
Sets a header on the request.
§Examples
Http::get("https://httpbin.org/get")
.body("<html>hi</html>")
.header("header-name", "header-value")
.build()
.then_send(Event::ReceiveResponse);
Sourcepub fn content_type(self, content_type: impl Into<Mime>) -> Self
pub fn content_type(self, content_type: impl Into<Mime>) -> Self
Sets the Content-Type header on the request.
§Examples
Http::get("https://httpbin.org/get")
.content_type(crux_http::http::mime::HTML)
.build()
.then_send(Event::ReceiveResponse);
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
Http::post("https://httpbin.org/post")
.body(serde_json::json!({"any": "Into<Body>"}))
.content_type(crux_http::http::mime::HTML)
.build()
.then_send(Event::ReceiveResponse);
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() };
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"),
]);
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 };
Http::post("https://httpbin.org/post")
.query(&query)
.expect("could not serialize query string")
.build()
.then_send(Event::ReceiveResponse);
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
Http::get("https://httpbin.org/redirect/2")
.middleware(crux_http::middleware::Redirect::default())
.build()
.then_send(Event::ReceiveResponse);
Sourcepub fn build(
self,
) -> RequestBuilder<Effect, Event, impl Future<Output = Result<Response<ExpectBody>, HttpError>>>
pub fn build( self, ) -> RequestBuilder<Effect, Event, impl Future<Output = Result<Response<ExpectBody>, HttpError>>>
Return the constructed Request
in a [crux_core::command::RequestBuilder
].
Sourcepub fn expect_string(self) -> RequestBuilder<Effect, Event, String>
pub fn expect_string(self) -> RequestBuilder<Effect, Event, String>
Sourcepub fn expect_json<T>(self) -> RequestBuilder<Effect, Event, T>where
T: DeserializeOwned + 'static,
pub fn expect_json<T>(self) -> RequestBuilder<Effect, Event, T>where
T: DeserializeOwned + 'static,
Decode a T
from a JSON response body prior to dispatching it to the apps update
function.
§Examples
#[derive(Deserialize)]
struct Response {
slideshow: Slideshow
}
#[derive(Deserialize)]
struct Slideshow {
author: String
}
Http::post("https://httpbin.org/json")
.expect_json::<Slideshow>()
.build()
.then_send(Event::ReceiveResponse);