Module request
Expand description
Set a header on the request.
The header value to be set may be provided as a fixed value when the
middleware is constructed, or determined dynamically based on the request
by a closure. See the MakeHeaderValue
trait for details.
§Example
Setting a header from a fixed value provided when the middleware is constructed:
use rama_http::layer::set_header::SetRequestHeaderLayer;
use rama_http::{Body, Request, Response, header::{self, HeaderValue}};
use rama_core::service::service_fn;
use rama_core::{Context, Service, Layer};
use rama_core::error::BoxError;
let mut svc = (
// Layer that sets `User-Agent: my very cool proxy` on requests.
//
// `if_not_present` will only insert the header if it does not already
// have a value.
SetRequestHeaderLayer::if_not_present(
header::USER_AGENT,
HeaderValue::from_static("my very cool proxy"),
),
).layer(http_client);
let request = Request::new(Body::empty());
let response = svc.serve(Context::default(), request).await?;
Setting a header based on a value determined dynamically from the request:
use rama_http::{Body, Request, Response, header::{self, HeaderValue}};
use rama_http::layer::set_header::SetRequestHeaderLayer;
use rama_core::service::service_fn;
use rama_core::{Context, Service, Layer};
use rama_core::error::BoxError;
fn date_header_value() -> HeaderValue {
// ...
}
let mut svc = (
// Layer that sets `Date` to the current date and time.
//
// `overriding` will insert the header and override any previous values it
// may have.
SetRequestHeaderLayer::overriding_fn(
header::DATE,
|| async move {
Some(date_header_value())
}
),
).layer(http_client);
let request = Request::new(Body::default());
let response = svc.serve(Context::default(), request).await?;
Structs§
- The public wrapper type for [
MakeHeaderValueFn
]. - Middleware that sets a header on the request.
- Layer that applies
SetRequestHeader
which adds a request header.
Traits§
- Trait for producing header values.