Expand description
§axum-content-negotiation
HTTP Content Negotiation middleware and extractor for Axum.
A set of Axum Layers and Extractors that enable content negotiation using Accept and Content-Type headers.
It implements schemaless serialization and deserialization content negotiation. Currently supported encodings are:
application/jsonapplication/cbor
§Installation
[dependencies]
axum-content-negotiation = "0.1"§Features
The following features can be enabled to include support for different encodings:
simd-json(default): Enables support forapplication/jsonencoding usingsimd-json.cbor(default): Enables support forapplication/cborencoding usingcbor4ii.json: Enables support forapplication/jsonencoding usingserde_json.
The following features enable the default content type when Accept header is missing or Accept: */* is present:
default-json(default): Assumesapplication/jsonas the default content type.default-cbor: Assumesapplication/cboras the default content type.
In order to customize your dependencies, you can enable or disable the features as follows:
[dependencies]
axum-content-negotiation = { version = "0.1", default-features = false, features = ["json", "default-json"] }§Usage
§Request payloads
The axum_content_negotiation::Negotiate is Extractor can be used in an Axum handlers to accept multiple Content-Type formats for the request body.
This extractor will attempt to deserialize the request body into the desired type based on the Content-Type header and a list of supported schemaless encodings.
use axum::{http::StatusCode, response::IntoResponse, routing::post, Router};
use axum_content_negotiation::Negotiate;
#[derive(serde::Deserialize, Debug)]
struct YourType {
name: String,
}
async fn handler(Negotiate(request_body): Negotiate<YourType>) -> impl IntoResponse {
(StatusCode::OK, format!("Received ${:?}", request_body))
}
let router: Router<()> = Router::new().route("/", post(handler));§Response payloads
In order to respond with the correct Content-Type header, the axum_content_negotiation::Negotiate also implements an IntoResponse trait,
but it requires axum_content_negotiation::NegotiateLayer in order to actually perform the serialization on the desired format.
use axum::{http::StatusCode, response::IntoResponse, routing::get, Router};
use axum_content_negotiation::{Negotiate, NegotiateLayer};
#[derive(serde::Serialize)]
struct YourType {
name: String,
}
async fn handler() -> impl IntoResponse {
let response = YourType {
name: "John".to_string(),
};
(StatusCode::OK, Negotiate(response))
}
let router: Router<()> = Router::new().route("/", get(handler)).layer(NegotiateLayer);§All together
use axum::{http::StatusCode, response::IntoResponse, routing::*, Router};
use axum_content_negotiation::{Negotiate, NegotiateLayer};
#[derive(serde::Deserialize, Debug)]
struct Input {
name: String,
}
#[derive(serde::Serialize)]
struct Output {
name: String,
}
async fn handler(Negotiate(request_body): Negotiate<Input>) -> impl IntoResponse {
let response = Output {
name: format!("Hello there, {}!", request_body.name),
};
(StatusCode::OK, Negotiate(response))
}
let router: Router<()> = Router::new().route("/", put(handler)).layer(NegotiateLayer);Structs§
- Negotiate
- Used either as an Extract or Response to negotiate the serialization format used.
- Negotiate
Layer - Layer responsible to convert a Negotiate response into the right serialization format based on the
Acceptheader. - Negotiate
Service - Serialize the stored Extension struct defined by a Negotiate into the right serialization format based on the
Acceptheader.