pub trait IntoReq<Encoding, Request, E> {
// Required method
fn into_req(self, path: &str, accepts: &str) -> Result<Request, E>;
}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<E, T, Request> IntoReq<Json, Request, E> for T
where
Request: ClientReq<E>,
T: Serialize + Send,
{
fn into_req(
self,
path: &str,
accepts: &str,
) -> Result<Request, E> {
// try to serialize the data
let data = serde_json::to_string(&self)
.map_err(|e| ServerFnErrorErr::Serialization(e.to_string()).into_app_error())?;
// and use it as the body of a POST request
Request::try_new_post(path, accepts, Json::CONTENT_TYPE, data)
}
}