From 08cc1222aa035f7714022185df0c4ff54a266227 Mon Sep 17 00:00:00 2001 From: Prabir Shrestha Date: Sun, 26 Apr 2020 14:10:50 -0700 Subject: [PATCH] add example for get and set http header --- examples/header.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 examples/header.rs diff --git a/examples/header.rs b/examples/header.rs new file mode 100644 index 000000000..867ed5584 --- /dev/null +++ b/examples/header.rs @@ -0,0 +1,27 @@ +use async_std::task; +use tide::{Request, Response, StatusCode}; + +fn main() -> Result<(), std::io::Error> { + task::block_on(async { + let mut app = tide::new(); + + app.at("/").get(|req: Request<()>| async move { + let content_type = req + .header(&"accept".parse().unwrap()) + .unwrap() + .get(0) + .unwrap() + .as_str(); + + let res = Response::new(StatusCode::Ok) + .body_string(content_type.to_owned()) + .set_header("content-type".parse().unwrap(), "text/html"); + + Ok(res) + }); + + app.listen("127.0.0.1:8080").await?; + + Ok(()) + }) +}