Returning status with JSON
So far, we have returned JSON and raw HTML. However, remember that our to-do applications return JSON with different statuses. To explore this concept, we can revisit our create
view in the src/views/to_do/create.rs
file, where we must return a created status with a JSON body. First, all our imports are the same as they were before, apart from the status and JSON structs from the Rocket framework with the following code:
use rocket::serde::json::Json; use rocket::response::status::Created;
With these imports, we can define the outline of our create
view function with the following code:
#[post("/create/<title>")] pub async fn create<'a>(token: JwToken, title: String, db: DB) -> Created<Json<ToDoItems>> { ...