From 2e350133a1fcc52a446e73baed6d0f534e764e10 Mon Sep 17 00:00:00 2001 From: shockrah Date: Thu, 18 Jun 2020 22:30:40 -0700 Subject: [PATCH] http_params::parse_params now using serde_json to convert the body into a Value set Why: The idea is that writing a body parser is both really annoying and not really optimal At this time I need some way of getting body params easily while endpionts are tested at some point we'll come up with a smaller footprint way of parsing http parameters Ideally one that also comes with the ability to combine parameters but, for now using serde_json suffices. --- server/src/http_params.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/server/src/http_params.rs b/server/src/http_params.rs index d47ff3d..80d6862 100644 --- a/server/src/http_params.rs +++ b/server/src/http_params.rs @@ -1,4 +1,5 @@ -use hyper::body::{to_bytes, Bytes, HttpBody}; +use serde_json::{self, Value}; +use hyper::body::{to_bytes, Bytes}; use hyper::Body; use std::collections::HashMap; use std::u8; @@ -75,10 +76,8 @@ async fn map_body(map: &mut HashMap<&str, &str>, body_raw: &mut Body) { } } - -// NOTE: we only need an async call because map body itself makes asynchronous calls -pub async fn parse_params<'q>(qs_raw: Option<&'q str>, body_raw: &mut Body) -> HashMap<&'q str, &'q str> { - let mut qs_map = map_qs(qs_raw); - map_body(&mut qs_map, body_raw).await; - qs_map +pub async fn parse_params(body_raw: &mut Body) -> Result { + let bytes: &[u8] = &*to_bytes(body_raw).await.unwrap(); // rarely fails + let mut values: Value = serde_json::from_slice(bytes)?; + Ok(values) } \ No newline at end of file