What happens in axum when a header is missing for a handler capturing a typed header?

443 views Asked by At

axum allows me to capture http headers in a handler with the TypedHeader struct. For example:

async fn handler(TypedHeader(if_none_match): TypedHeader<IfNoneMatch>) -> String {
    let etag = "foo".parse::<ETag>().unwrap();
    if if_none_match.precondition_passes(&etag) {
        String::from("success")
    } else {
        String::from("failure")
    }
}

Now I'm wondering what happens if I get a request where the If-None-Match header is missing. With some debugging, I found out that the handler will still be called and the precondition will pass. However, I'm wondering what the instance of IfNoneMatch is now which I'm receiving? How is axum setting this instance and how can I determine whether the header was originally missing? I used the If-None-Match http header as an example here, but I would be interested in a general answer.

0

There are 0 answers