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};
caps.http
.post("https://httpbin.org/post")
.body("<html>hi</html>")
.header("custom-header", "value")
.content_type(HTML)
.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
Sets a header on the request.
§Examples
caps.http
.get("https://httpbin.org/get")
.body("<html>hi</html>")
.header("header-name", "header-value")
.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
caps.http
.get("https://httpbin.org/get")
.content_type(mime::HTML)
.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
use serde_json::json;
use crux_http::http::mime;
caps.http
.post("https://httpbin.org/post")
.body(json!({"any": "Into<Body>"}))
.content_type(mime::HTML)
.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() };
caps.http
.post("https://httpbin.org/post")
.body_json(data)
.expect("could not serialize body")
.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 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 };
caps.http
.post("https://httpbin.org/post")
.query(&query)
.expect("could not serialize query string")
.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
caps.http
.get("https://httpbin.org/redirect/2")
.middleware(crux_http::middleware::Redirect::default())
.send(Event::ReceiveResponse)
sourcepub fn expect_string(self) -> RequestBuilder<Event, String>
pub fn expect_string(self) -> RequestBuilder<Event, String>
Decode a String from the response body prior to dispatching it to the apps update
function.
This has no effect when used with the async API.
§Examples
enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<String>>) }
caps.http
.post("https://httpbin.org/json")
.expect_string()
.send(Event::ReceiveResponse)
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
}
enum Event { ReceiveResponse(crux_http::Result<crux_http::Response<Slideshow>>) }
caps.http
.post("https://httpbin.org/json")
.expect_json::<Slideshow>()
.send(Event::ReceiveResponse)
sourcepub fn send<F>(self, make_event: F)
pub fn send<F>(self, make_event: F)
Sends the constructed Request
and returns its result as an update Event
When finished, the response will wrapped in an event using make_event
and
dispatched to the app’s `update function.
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