also adding json_with_status
This commit is contained in:
@@ -23,14 +23,7 @@ impl Responder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn json<T: Serialize>(value: &T) -> Result<Response<Full<Bytes>>, Infallible> {
|
pub fn json<T: Serialize>(value: &T) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||||
match serde_json::to_vec(value) {
|
Self::json_with_status(StatusCode::OK, value)
|
||||||
Ok(bytes) => Ok(Response::builder()
|
|
||||||
.status(StatusCode::OK)
|
|
||||||
.header(CONTENT_TYPE, "application/json")
|
|
||||||
.body(Full::new(Bytes::from(bytes)))
|
|
||||||
.unwrap()),
|
|
||||||
Err(e) => Self::internal_error(format!("JSON Serialization Error: {}", e)),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn redirect(url: &str) -> Result<Response<Full<Bytes>>, Infallible> {
|
pub fn redirect(url: &str) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||||
@@ -66,4 +59,18 @@ impl Responder {
|
|||||||
.body(Full::new(body.into()))
|
.body(Full::new(body.into()))
|
||||||
.unwrap())
|
.unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn json_with_status<T: Serialize>(
|
||||||
|
status: StatusCode,
|
||||||
|
value: &T,
|
||||||
|
) -> Result<Response<Full<Bytes>>, Infallible> {
|
||||||
|
match serde_json::to_vec(value) {
|
||||||
|
Ok(bytes) => Ok(Response::builder()
|
||||||
|
.status(status)
|
||||||
|
.header(CONTENT_TYPE, "application/json")
|
||||||
|
.body(Full::new(Bytes::from(bytes)))
|
||||||
|
.unwrap()),
|
||||||
|
Err(e) => Self::internal_error(format!("JSON Serialization Error: {}", e)),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user