Skip to content

bahlo/axum-accept

Repository files navigation

axum-accept

CI crates.io docs.rs License

Typed accept negotiation for axum, following RFC7231.

Example

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
<empty> Default variant HTTP 406 (Not Acceptable)
*/* Default variant First variant

License

Licensed under either of

at your option.

About

Typed accept negotiation for axum.

Topics

Resources

License

Apache-2.0, MIT licenses found

Licenses found

Apache-2.0
LICENSE-APACHE
MIT
LICENSE-MIT

Stars

Watchers

Forks

Languages