From 32ca8e951d7e5b712e20f1ff37f4d0a6eb868e7c Mon Sep 17 00:00:00 2001 From: Luke Anderson Date: Thu, 5 Sep 2019 00:36:06 +1000 Subject: [PATCH] Updated content-type acceptance and returning, mainly for /spec/eth2_config --- beacon_node/rest_api/src/beacon.rs | 5 +-- beacon_node/rest_api/src/response_builder.rs | 35 ++++++++++++-------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/beacon_node/rest_api/src/beacon.rs b/beacon_node/rest_api/src/beacon.rs index ae112883e..70b3f3ee9 100644 --- a/beacon_node/rest_api/src/beacon.rs +++ b/beacon_node/rest_api/src/beacon.rs @@ -276,10 +276,7 @@ pub fn get_current_finalized_checkpoint( Ok(success_response(Body::from(json))) } -/// HTTP handler to return a `BeaconState` at a given `root` or `slot`. -/// -/// Will not return a state if the request slot is in the future. Will return states higher than -/// the current head by skipping slots. +/// HTTP handler to return a `BeaconState` at the genesis block. pub fn get_genesis_state(req: Request) -> ApiResult { let beacon_chain = get_beacon_chain_from_request::(&req)?; diff --git a/beacon_node/rest_api/src/response_builder.rs b/beacon_node/rest_api/src/response_builder.rs index 9b8819996..c1df4892c 100644 --- a/beacon_node/rest_api/src/response_builder.rs +++ b/beacon_node/rest_api/src/response_builder.rs @@ -26,24 +26,31 @@ impl ResponseBuilder { } pub fn body(self, item: &T) -> ApiResult { - let body: Body = match self.encoding { - Encoding::JSON => Body::from(serde_json::to_string(&item).map_err(|e| { - ApiError::ServerError(format!( - "Unable to serialize response body as JSON: {:?}", - e - )) - })?), - Encoding::SSZ => Body::from(item.as_ssz_bytes()), - Encoding::YAML => Body::from(serde_yaml::to_string(&item).map_err(|e| { - ApiError::ServerError(format!( - "Unable to serialize response body as YAML: {:?}", - e - )) - })?), + let (body, content_type) = match self.encoding { + Encoding::JSON => ( + Body::from(serde_json::to_string(&item).map_err(|e| { + ApiError::ServerError(format!( + "Unable to serialize response body as JSON: {:?}", + e + )) + })?), + "application/json", + ), + Encoding::SSZ => (Body::from(item.as_ssz_bytes()), "application/ssz"), + Encoding::YAML => ( + Body::from(serde_yaml::to_string(&item).map_err(|e| { + ApiError::ServerError(format!( + "Unable to serialize response body as YAML: {:?}", + e + )) + })?), + "application/ssz", + ), }; Response::builder() .status(StatusCode::OK) + .header("content-type", content_type) .body(Body::from(body)) .map_err(|e| ApiError::ServerError(format!("Failed to build response: {:?}", e))) }