[−][src]Module tide::body
Types for working directly with the bodies of requests and responses.
This module includes types like Json
, which can be used to automatically (de)serialize bodies
using serde_json
.
Examples
Read/Write Strings
and Bytes
from/to bodies:
use tide::body; async fn echo_string(msg: body::Str) -> String { println!("String: {}", *msg); format!("{}", *msg) } async fn echo_string_lossy(msg: body::StrLossy) -> String { println!("String: {}", *msg); format!("{}", *msg) } async fn echo_bytes(msg: body::Bytes) -> body::Bytes { println!("Bytes: {:?}", *msg); msg } app.at("/echo/string").post(echo_string); app.at("/echo/string_lossy").post(echo_string_lossy); app.at("/echo/bytes").post(echo_bytes);
Using serde_json
to automatically (de)serialize bodies into/from structs:
#[macro_use] extern crate serde_derive; use tide::body; #[derive(Serialize, Deserialize, Clone, Debug)] struct Message { author: Option<String>, contents: String, } async fn echo_json(msg: body::Json<Message>) -> body::Json<Message> { println!("JSON: {:?}", *msg); msg } async fn echo_form(msg: body::Form<Message>) -> body::Form<Message> { println!("Form: {:?}", *msg); msg } app.at("/echo/json").post(echo_json); app.at("/echo/form").post(echo_form);
Structs
Body | The raw contents of an http request or response. |
BodyChunk | |
Bytes | |
Form | A wrapper for form encoded (application/x-www-form-urlencoded) (de)serialization of bodies. |
Json | A wrapper for json (de)serialization of bodies. |
MultipartForm | A wrapper for multipart form |
Str | |
StrLossy |