Getting started
How to install
Add actix-json-response
to your dependencies:
[]
# ...
= "4"
= "0.1"
Quickstart
actix-json-response
exposes the JsonResponse
type which implements Actix's Responder
trait. It is generic and receives a type parameter that must implement Serde's Serialize
trait:
use ;
use JsonResponse;
use Serialize;
async
async
By default, the response will have status code 200
. If you need the response to have a different status code, you can use the with_status_code
method that receives an Actix's StatusCode
:
use actix_web::http::StatusCode;
#[get("/a/{name}")]
async fn index(name: web::Path<String>) -> Result<JsonResponse<MyObj>> {
let my_obj = MyObj {
name: name.to_string(),
};
Ok(JsonResponse::from(my_obj).with_status_code(StatusCode::CREATED)) // The response will have status code 201 in this case
}
License
Distributed under the terms of MIT license and Apache license.