{"record":{"id":"a502586daa889049","repo":"seanmonstar/warp","slug":"invalid-header","errorCode":null,"errorMessage":"invalid_header","messagePattern":"invalid_header","errorType":"http","errorClass":"InvalidHeader","httpStatus":400,"severity":"warning","filePath":"src/filters/header.rs","lineNumber":94,"sourceCode":"pub fn optional<T>(\n    name: &'static str,\n) -> impl Filter<Extract = One<Option<T>>, Error = Rejection> + Copy\nwhere\n    T: FromStr + Send + 'static,\n{\n    filter_fn_one(move |route| {\n        tracing::trace!(\"optional({:?})\", name);\n        let result = route.headers().get(name).map(|value| {\n            value\n                .to_str()\n                .map_err(|_| reject::invalid_header(name))?\n                .parse::<T>()\n                .map_err(|_| reject::invalid_header(name))\n        });\n\n        match result {\n            Some(Ok(t)) => future::ok(Some(t)),\n            Some(Err(e)) => future::err(e),\n            None => future::ok(None),\n        }\n    })\n}\n\npub(crate) fn optional2<T>() -> impl Filter<Extract = One<Option<T>>, Error = Infallible> + Copy\nwhere\n    T: Header + Send + 'static,\n{\n    filter_fn_one(move |route| future::ready(Ok(route.headers().typed_get())))\n}\n\n/* TODO\npub fn exact2<T>(header: T) -> impl FilterClone<Extract=(), Error=Rejection>\nwhere\n    T: Header + PartialEq + Clone + Send,\n{\n    filter_fn(move |route| {","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/seanmonstar/warp/blob/ff34d7213ed55ec342304aa7ff6ac4b351da9e66/src/filters/header.rs#L76-L112","documentation":"This is warp's reject::invalid_header rejection raised by header::optional (src/filters/header.rs:94). The named header was present in the request, but its value failed to parse into the target type T (via FromStr). Unlike a missing header — which optional() turns into None — a present-but-unparseable header is an error rejection. Note the public optional2 variant maps the failure into Ok(None) instead.","triggerScenarios":"warp::header::<T>(\"x-custom\") or the optional()/optional2 chain where T's FromStr::from_str fails, e.g. warp::header::<u64>(\"content-length\") with a non-numeric value, a datetime header in an unexpected format, or a header value with invalid characters for the type.","commonSituations":"Client sends content-length or a custom numeric header with garbage/extra whitespace; version drift where the client changed a header format; internationalized or URL-encoded header values; parsing Date/If-Modified-Since headers whose format differs from the expected RFC form.","solutions":["Inspect the actual header value the client sends (curl -v) and compare with T's FromStr expectations","Trim whitespace or normalize the value client-side before sending","Use header::optional2 (or your own map over optional()) to treat unparseable as None instead of a rejection","Use a custom newtype with a lenient FromStr implementation to accept alternate formats","Return a 400 with a clear message by handling the invalid_header rejection in recover()"],"exampleFix":"// before\nlet len = warp::header::<u64>(\"x-items\");\n// after (tolerate missing or malformed header)\nlet len = warp::header::optional2::<u64>(\"x-items\")\n    .map(|opt| opt.unwrap_or(0));","handlingStrategy":"validation","validationCode":"// validate header format client-side before sending\nconst items = \"42\";\nif (!/^\\d+$/.test(items)) throw new Error(\"x-items must be a positive integer\");\nheaders[\"x-items\"] = items;","typeGuard":"fn parse_header_u64(v: &str) -> Option<u64> { v.trim().parse::<u64>().ok() }","tryCatchPattern":".recover(|rej: warp::Rejection| async move {\n    if let Some(e) = rej.find::<warp::reject::InvalidHeader>() {\n        Ok(warp::reply::with_status(\n            format!(\"invalid value for header: {}\", e.name()),\n            warp::http::StatusCode::BAD_REQUEST,\n        ))\n    } else {\n        Err(rej)\n    }\n})","preventionTips":["Document the exact header format expected by each route","Trim/normalize header values on the client before sending","Use optional2 when an unparseable header should degrade to None","Write integration tests that exercise each typed header extraction","Handle InvalidHeader in recover() to return a descriptive 400 instead of a bare rejection"],"tags":["http","headers","parsing","warp"],"backgroundTag":"invalid-header-value","analyzedSha":"ff34d7213ed55ec342304aa7ff6ac4b351da9e66","analyzedAt":"2026-09-09T16:57:46.316Z","contentChangedAt":"2026-09-09T16:57:46.316Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}