use axum::{
body::Body,
http::{Method, Request, StatusCode, header::CONTENT_TYPE},
middleware::Next,
response::{IntoResponse, Response},
};
pub async fn require_json_content_type(
req: Request<Body>,
next: Next,
) -> Result<Response, Response> {
if req.method() != Method::POST {
return Ok(next.run(req).await);
}
let content_type = req.headers().get(CONTENT_TYPE).and_then(|v| v.to_str().ok()).unwrap_or("");
if !content_type.starts_with("application/json") {
let body = serde_json::json!({
"errors": [{
"message": "Content-Type must be application/json",
"extensions": { "code": "UNSUPPORTED_MEDIA_TYPE" }
}]
});
return Err((
StatusCode::UNSUPPORTED_MEDIA_TYPE,
[(CONTENT_TYPE, "application/json")],
serde_json::to_string(&body).unwrap_or_else(|_| {
r#"{"errors":[{"message":"Unsupported Media Type"}]}"#.to_owned()
}),
)
.into_response());
}
Ok(next.run(req).await)
}