leptos::server_fn::codec

Trait IntoReq

Source
pub trait IntoReq<Encoding, Request, CustErr> {
    // Required method
    fn into_req(
        self,
        path: &str,
        accepts: &str,
    ) -> Result<Request, ServerFnError<CustErr>>;
}
Expand description

Serializes a data type into an HTTP request, on the client.

Implementations use the methods of the ClientReq trait to convert data into a request body. They are often quite short, usually consisting of just two steps:

  1. Serializing the data into some String, Bytes, or Stream.
  2. Creating a request with a body of that type.

For example, here’s the implementation for Json.

impl<CustErr, T, Request> IntoReq<Json, Request, CustErr> for T
where
    Request: ClientReq<CustErr>,
    T: Serialize + Send,
{
    fn into_req(
        self,
        path: &str,
        accepts: &str,
    ) -> Result<Request, ServerFnError<CustErr>> {
        // try to serialize the data
        let data = serde_json::to_string(&self)
            .map_err(|e| ServerFnError::Serialization(e.to_string()))?;
        // and use it as the body of a POST request
        Request::try_new_post(path, accepts, Json::CONTENT_TYPE, data)
    }
}

Required Methods§

Source

fn into_req( self, path: &str, accepts: &str, ) -> Result<Request, ServerFnError<CustErr>>

Attempts to serialize the arguments into an HTTP request.

Implementors§

Source§

impl<CustErr, S, T, Request> IntoReq<StreamingJson, Request, CustErr> for S
where Request: ClientReq<CustErr>, S: Stream<Item = T> + Send + 'static, T: Serialize + 'static,

Source§

impl<CustErr, T, Request> IntoReq<GetUrl, Request, CustErr> for T
where Request: ClientReq<CustErr>, T: Serialize + Send,

Source§

impl<CustErr, T, Request> IntoReq<Json, Request, CustErr> for T
where Request: ClientReq<CustErr>, T: Serialize + Send,

Source§

impl<CustErr, T, Request> IntoReq<PostUrl, Request, CustErr> for T
where Request: ClientReq<CustErr>, T: Serialize + Send,

Source§

impl<CustErr, T, Request> IntoReq<Streaming, Request, CustErr> for T
where Request: ClientReq<CustErr>, T: Stream<Item = Bytes> + Send + Sync + 'static,

Source§

impl<CustErr, T, Request> IntoReq<StreamingText, Request, CustErr> for T
where Request: ClientReq<CustErr>, T: Into<TextStream>,