Echos back HTTP headers in the form of a JSON body.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

26 lines
683 B

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use warp::http::HeaderMap;
use warp::Filter;
#[derive(Serialize, Deserialize)]
struct ResponseBody {
headers: HashMap<String, String>,
}
#[tokio::main]
async fn main() {
let get_req = warp::path!("get")
.and(warp::header::headers_cloned())
.and_then(get_fn);
warp::serve(get_req).run(([127, 0, 0, 1], 3030)).await;
}
async fn get_fn(headers: HeaderMap) -> Result<impl warp::Reply, warp::Rejection> {
let headers: HashMap<String, String> = headers
.iter()
.map(|(name, value)| (name.to_string(), value.to_str().unwrap().to_string()))
.collect();
Ok(warp::reply::json(&ResponseBody { headers }))
}