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:
- Serializing the data into some
String
,Bytes
, orStream
. - 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)
}
}