https://github.com/bahlo/axum-accept
Typed accept negotiation for axum.
https://github.com/bahlo/axum-accept
accept axum header mediatype
Last synced: 11 months ago
JSON representation
Typed accept negotiation for axum.
- Host: GitHub
- URL: https://github.com/bahlo/axum-accept
- Owner: bahlo
- License: apache-2.0
- Created: 2025-07-07T13:14:55.000Z (12 months ago)
- Default Branch: main
- Last Pushed: 2025-07-16T19:19:48.000Z (12 months ago)
- Last Synced: 2025-07-17T16:08:34.298Z (12 months ago)
- Topics: accept, axum, header, mediatype
- Language: Rust
- Homepage: https://docs.rs/axum-accept
- Size: 65.4 KB
- Stars: 3
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# axum-accept
[](https://github.com/bahlo/axum-accept/actions/workflows/ci.yml)
[](https://crates.io/crates/axum-accept)
[](https://docs.rs/axum-accept/)
[](LICENSE-APACHE)
Typed accept negotiation for axum, following [RFC7231](https://www.rfc-editor.org/rfc/rfc7231).
## Example
```rust
use axum::{extract::Json, response::{IntoResponse, Response}};
use axum_accept::AcceptExtractor;
use serde_json::json;
#[derive(AcceptExtractor, Default)]
enum Accept {
#[accept(mediatype="text/plain")]
TextPlain,
#[default]
#[accept(mediatype="application/json")]
ApplicationJson,
}
async fn my_handler(accept: Accept) -> Response {
match accept {
Accept::TextPlain => "hello world".into_response(),
Accept::ApplicationJson => Json(json!({ "content": "hello_world" })).into_response(),
}
}
```
## Edge cases
Setting a default is recommended as it indicates behaviour more explicitly in
your code.
This is how axum-accept behaves on edge cases:
| Accept | Has default | No default |
| --------- | ------------------------- | ------------------------- |
| `` | Default variant | HTTP 406 (Not Acceptable) |
| `*/*` | Default variant | First variant |
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.