pub fn json_success_resp_with_code<B, D>(
code: StatusCode,
data: &D,
) -> Result<Response<B>>
Expand description
Generates a success JSON response with the provided data and the status code.
It generates JSON response in the following JSON format:
{
"status": "success",
"code": "<status_code>",
"data": "<data>"
}
ยงExamples
use hyper::{Body, Request, Response, StatusCode};
use json_response::{json_success_resp_with_code};
async fn list_users_handler(_: Request<Body>) -> Result<Response<Body>, routerify::Error> {
// Fetch response data from somewhere.
let users = ["Alice", "John"];
// Generate a success JSON response with the data in the following format:
// { "status": "success", code: 201, data: ["Alice", "John"] }
json_success_resp_with_code(StatusCode::CREATED, &users)
}