Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
tower-conneg
Tower middleware for HTTP content negotiation on both client and server.
Features
- Server-side: Deserialize request bodies via
Content-Type, serialize responses viaAccept - Client-side: Serialize request bodies, deserialize responses via
Content-Type - Multiple formats: JSON,
MessagePack, CBOR, XML, TOML, Form, Postcard, BSON, plain text - Framework integrations: Axum, Poem, Salvo, Viz
OpenAPIhelpers: Utoipa request bodies, responses, and negotiation errors- Type-safe: Explicit format threading through handler signatures
- Serde-based: Uses
erased-serdefor type erasure at the format level
Quick Start (Server with Axum)
use ;
use ;
use ;
async
let config = builder
.formats
.build;
let app = new
.route
.layer;
For endpoints without request bodies (GET, DELETE), use Negotiate<()>:
async
Quick Start (Client)
use ;
let config = builder
.formats
.fallback_format
.build;
let client = new
.layer
.service;
Available Formats
| Format | Feature Flag | Media Type |
|---|---|---|
| JSON | json (default) |
application/json |
MessagePack |
msgpack |
application/msgpack |
| CBOR | cbor |
application/cbor |
| XML | xml |
application/xml |
| TOML | toml |
application/toml |
| Form | form |
application/x-www-form-urlencoded |
| Postcard | postcard |
application/x-postcard |
| BSON | bson |
application/bson |
| Plain Text | plain |
text/plain |
| HTML | plain |
text/html |
Framework Integrations
| Framework | Feature Flag | Notes |
|---|---|---|
| Axum | axum |
Negotiate<T> implements FromRequest/FromRequestParts, NegotiateResponse implements IntoResponse |
| Poem | poem |
Native extractor/responder support |
| Salvo | salvo |
Native Extractible support |
| Viz | viz |
Native extractor support |
| Hyper Client | hyper-client |
Client-side negotiation |
| Utoipa | utoipa |
OpenAPI content, request body, response, and error helpers |
OpenAPI with Utoipa
Enable the utoipa feature to document the same negotiated media types your handlers accept and
return:
use OpenApiFormats;
use ;
use ToSchema;
let docs = from_media_types;
let create_user = new
.request_body
.response
.response
.response
.build;
let paths = new
.path
.build;
The generated OpenAPI operation includes every selected format media type:
Use request_body_with_content and response_with_content when you
need Utoipa Content metadata such as examples, encodings, or extensions.
See cargo run --example openapi --features "utoipa,json,msgpack" for a runnable example.
Design
The library uses an extractor/responder pattern where the negotiated format flows explicitly through handler signatures:
- Middleware parses
AcceptandContent-Typeheaders Negotiate<T>extractor deserializes the request body and captures the format- Handler calls
.respond(value)to create aNegotiateResponse<T> IntoResponseserializes using the captured format
This approach avoids heap allocation for response values and makes data flow visible in type signatures.
License
MIT