diff options
author | Alex Auvolat <alex@adnab.me> | 2022-05-25 17:05:56 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2022-05-25 17:09:33 +0200 |
commit | ff06d3f0829464863e64ed55471f2caa13bed191 (patch) | |
tree | 2ba08f439721c28d326badc0a33f148ee1fbdce1 /src/api/helpers.rs | |
parent | 93eab8eaa3927b99626fee4a747a0f9f041cafdb (diff) | |
download | garage-ff06d3f0829464863e64ed55471f2caa13bed191.tar.gz garage-ff06d3f0829464863e64ed55471f2caa13bed191.zip |
Fix Content-Type headers for {admin,k2v} errors and admin responsesfix/admin-api-content-type
Fix #315
Diffstat (limited to 'src/api/helpers.rs')
-rw-r--r-- | src/api/helpers.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/src/api/helpers.rs b/src/api/helpers.rs index 9fb12dbe..642dbc42 100644 --- a/src/api/helpers.rs +++ b/src/api/helpers.rs @@ -1,4 +1,4 @@ -use hyper::{Body, Request}; +use hyper::{Body, Request, Response}; use idna::domain_to_unicode; use serde::{Deserialize, Serialize}; @@ -144,6 +144,14 @@ pub async fn parse_json_body<T: for<'de> Deserialize<'de>>(req: Request<Body>) - Ok(resp) } +pub fn json_ok_response<T: Serialize>(res: &T) -> Result<Response<Body>, Error> { + let resp_json = serde_json::to_string_pretty(res).map_err(garage_util::error::Error::from)?; + Ok(Response::builder() + .status(hyper::StatusCode::OK) + .header(http::header::CONTENT_TYPE, "application/json") + .body(Body::from(resp_json))?) +} + #[cfg(test)] mod tests { use super::*; |