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.
This commit is contained in:
shockrah 2020-06-18 22:30:40 -07:00
parent 250efd71d9
commit 2e350133a1

View File

@ -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<Value, serde_json::error::Error> {
let bytes: &[u8] = &*to_bytes(body_raw).await.unwrap(); // rarely fails
let mut values: Value = serde_json::from_slice(bytes)?;
Ok(values)
}