Skip to content

Commit

Permalink
json example
Browse files Browse the repository at this point in the history
Signed-off-by: Yoshua Wuyts <yoshuawuyts@gmail.com>
  • Loading branch information
yoshuawuyts committed Nov 27, 2019
1 parent 6665925 commit 37d9ff0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tide"
version = "0.3.0"
version = "0.4.0"
description = "Serve the web – HTTP server framework"
authors = [
"Aaron Turon <aturon@mozilla.com>",
Expand Down Expand Up @@ -38,7 +38,6 @@ serde_qs = "0.5.0"
async-std = { version = "1.0.1", features = ["unstable"] }
hyper = { version = "0.12.35", default-features = false }
bytes = "0.4.12"
anyhow = "1.0.19"
pin-project-lite = "0.1.0"
mime = "0.3.14"

Expand Down
25 changes: 25 additions & 0 deletions examples/json.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use async_std::task;
use serde::{Serialize, Deserialize};

#[derive(Deserialize, Serialize)]
struct Cat {
name: String
}

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
task::block_on(async {
let app = tide::new();

app.at("/submit").post(|req: tide::Request<()>| async {
let cat: Cat = req.body_json().await.unwrap();
println!("cat name: {}", cat.name);

let cat = Cat { name: "chashu".into() };
let res = tide::Response::new(200).body_json(&cat).unwrap();
res
});

app.listen("127.0.0.1:8080").await?;
Ok(())
})
}

0 comments on commit 37d9ff0

Please sign in to comment.