From 619a564cbab4f2a6ea6528dab9719add1a23a4dd Mon Sep 17 00:00:00 2001 From: Jarrod Overson Date: Thu, 31 Aug 2023 17:57:15 -0400 Subject: [PATCH] fix: fixed source of "could not decode body as JSON" warnings --- .../src/triggers/http/routers/raw.rs | 17 +++++++++-------- .../src/triggers/http/routers/rest.rs | 6 +++--- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/crates/wick/wick-runtime/src/triggers/http/routers/raw.rs b/crates/wick/wick-runtime/src/triggers/http/routers/raw.rs index a7009d2b..647993c2 100644 --- a/crates/wick/wick-runtime/src/triggers/http/routers/raw.rs +++ b/crates/wick/wick-runtime/src/triggers/http/routers/raw.rs @@ -122,14 +122,15 @@ impl RawHandler { Ok(b) => { let bytes = b.join(&0); trace!(?bytes, "http:codec:json:bytes"); - serde_json::from_slice::(&bytes).map_or_else( - |_| { - let _ = tx.send(Packet::err("body", "Could not decode body as JSON")); - }, - |value| { - let _ = tx.send(Packet::encode("body", value)); - }, - ); + let packet = if bytes.is_empty() { + Packet::encode("body", None::) + } else { + serde_json::from_slice::>(&bytes).map_or_else( + |e| Packet::err("body", e.to_string()), + |value| Packet::encode("body", Some(value)), + ) + }; + let _ = tx.send(packet); } Err(e) => { let _ = tx.send(Packet::err("body", e.to_string())); diff --git a/crates/wick/wick-runtime/src/triggers/http/routers/rest.rs b/crates/wick/wick-runtime/src/triggers/http/routers/rest.rs index 42d2ffcd..b5464aad 100644 --- a/crates/wick/wick-runtime/src/triggers/http/routers/rest.rs +++ b/crates/wick/wick-runtime/src/triggers/http/routers/rest.rs @@ -180,10 +180,10 @@ impl RestHandler { span.in_scope(|| trace!(route = %uri, len=body_bytes.len(), "body")); if !matches!(method, HttpMethod::Get) { - let payload: serde_json::Value = if body.trim().is_empty() { - serde_json::Value::Null + let payload: Option = if body.trim().is_empty() { + None } else { - serde_json::from_str(&body).map_err(HttpError::InvalidBody)? + Some(serde_json::from_str(&body).map_err(HttpError::InvalidBody)?) }; packets.push(Packet::encode("input", payload));