use serde_json::{self, Value}; use hyper::http::HeaderValue; use hyper::Response; use hyper::Body; use std::collections::HashMap; const APP_JSON_HEADER: &'static str = "application/json"; const CONTENT_TYPE: &'static str = "Content-Type"; pub fn set_json_body(response: &mut Response
, values: Value) { response.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static(APP_JSON_HEADER)); *response.body_mut() = Body::from(values.to_string()); } pub fn parse_query_string<'qs>(string: &'qs str) -> HashMap<&str, &str> { let mut map: HashMap<&str, &str> = HashMap::new(); // get pairs of [key1=value1, key2=value2, ...] for pair in string.split('&') { let kv: Vec<&str> = pair.split('=').collect(); match (kv.get(0), kv.get(1)) { // only if the format is some_key=some_value do we actually care about it (Some(key), Some(value)) => { map.insert(key, value); }, // ignore all non-pairs _ => continue }; } return map; } // Pulls out Option