diff --git a/axum/src/typed_header.rs b/axum/src/typed_header.rs index 1962a08074..4b7f1590c0 100644 --- a/axum/src/typed_header.rs +++ b/axum/src/typed_header.rs @@ -61,17 +61,19 @@ where type Rejection = TypedHeaderRejection; async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { - match parts.headers.typed_try_get::() { - Ok(Some(value)) => Ok(Self(value)), - Ok(None) => Err(TypedHeaderRejection { + let mut values = parts.headers.get_all(T::name()).iter(); + let is_missing = values.size_hint() == (0, Some(0)); + T::decode(&mut values) + .map(Self) + .map_err(|err| TypedHeaderRejection { name: T::name(), - reason: TypedHeaderRejectionReason::Missing, - }), - Err(err) => Err(TypedHeaderRejection { - name: T::name(), - reason: TypedHeaderRejectionReason::Error(err), - }), - } + reason: if is_missing { + // Report a more precise rejection for the missing header case. + TypedHeaderRejectionReason::Missing + } else { + TypedHeaderRejectionReason::Error(err) + }, + }) } }