crux_http::command

Struct RequestBuilder

Source
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,

Source

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);
Source

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);
Source

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);
Source

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);
Source

pub fn body_string(self, string: String) -> Self

Pass a string as the request body.

§Mime

The encoding is set to text/plain; charset=utf-8.

§Examples
Http::post("https://httpbin.org/post")
    .body_string("hello_world".to_string())
    .build()
    .then_send(Event::ReceiveResponse);
Source

pub fn body_bytes(self, bytes: impl AsRef<[u8]>) -> Self

Pass bytes as the request body.

§Mime

The encoding is set to application/octet-stream.

§Examples
Http::post("https://httpbin.org/post")
    .body_bytes(b"hello_world".to_owned())
    .build()
    .then_send(Event::ReceiveResponse);
Source

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);
Source

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);
Source

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);
Source

pub fn build( self, ) -> RequestBuilder<Effect, Event, impl Future<Output = Result<Response<ExpectBody>, HttpError>>>

Return the constructed Request in a [crux_core::command::RequestBuilder].

Source

pub fn expect_string(self) -> RequestBuilder<Effect, 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
Http::post("https://httpbin.org/json")
    .expect_string()
    .build()
    .then_send(Event::ReceiveResponse);
Source

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);

Trait Implementations§

Source§

impl<Effect, Event> Debug for RequestBuilder<Effect, Event>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Effect, Event, ExpectBody> Freeze for RequestBuilder<Effect, Event, ExpectBody>

§

impl<Effect, Event, ExpectBody = Vec<u8>> !RefUnwindSafe for RequestBuilder<Effect, Event, ExpectBody>

§

impl<Effect, Event, ExpectBody> Send for RequestBuilder<Effect, Event, ExpectBody>
where Effect: Send,

§

impl<Effect, Event, ExpectBody = Vec<u8>> !Sync for RequestBuilder<Effect, Event, ExpectBody>

§

impl<Effect, Event, ExpectBody> Unpin for RequestBuilder<Effect, Event, ExpectBody>
where Effect: Unpin,

§

impl<Effect, Event, ExpectBody = Vec<u8>> !UnwindSafe for RequestBuilder<Effect, Event, ExpectBody>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T