Struct poem::web::Json

source · []
pub struct Json<T>(pub T);
Expand description

JSON extractor and response.

To extract the specified type of JSON from the body, T must implement serde::Deserialize.

Errors

use poem::{
    handler,
    http::{header, Method, StatusCode},
    post,
    test::TestClient,
    web::Json,
    Endpoint, Request, Route,
};
use serde::Deserialize;

#[derive(Deserialize)]
struct User {
    name: String,
}

#[handler]
async fn index(Json(user): Json<User>) -> String {
    format!("welcome {}!", user.name)
}

let app = Route::new().at("/", post(index));
let cli = TestClient::new(app);

let resp = cli
    .post("/")
    .header(header::CONTENT_TYPE, "application/json")
    .body(r#"{"name": "foo"}"#)
    .send()
    .await;
resp.assert_status_is_ok();
resp.assert_text("welcome foo!").await;

Response

To serialize the specified type to JSON, T must implement serde::Serialize.

use poem::{
    get, handler, http::StatusCode, test::TestClient, web::Json, Endpoint, Request, Route,
};
use serde::Serialize;

#[derive(Serialize)]
struct User {
    name: String,
}

#[handler]
async fn index() -> Json<User> {
    Json(User {
        name: "foo".to_string(),
    })
}

let app = Route::new().at("/", get(index));
let cli = TestClient::new(app);

let resp = cli.get("/").send().await;
resp.assert_status_is_ok();
resp.assert_text(r#"{"name":"foo"}"#).await;

Tuple Fields

0: T

Trait Implementations

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Returns the “default value” for a type. Read more
The resulting type after dereferencing.
Dereferences the value.
Mutably dereferences the value.
Extract from request head and body.
Consume itself and return Response.
Wrap an impl IntoResponse to add a header. Read more
Wrap an impl IntoResponse to with a new content type. Read more
Wrap an impl IntoResponse to set a status code. Read more
Wrap an impl IntoResponse to set a body. Read more
This method tests for self and other values to be equal, and is used by ==. Read more
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason. Read more

Auto Trait Implementations

Blanket Implementations

A guard object containing the value and keeping it alive. Read more
The loading method. Read more
Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more
The equivalent of [Access::load].
Compare self to key and return true if they are equal.

Returns the argument unchanged.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
Attaches the current Context to this type, returning a WithContext wrapper. Read more
Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Consumes this value returns a poem::Result<T>.
Should always be Self
The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more