Skip to main content

Module request

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::{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"),
    ),
).into_layer(http_client);

let request = Request::new(Body::empty());

let response = svc.serve(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::{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 || {
            Some(date_header_value())
        }
    ),
).into_layer(http_client);

let request = Request::new(Body::default());

let response = svc.serve(request).await?;

Structs§

BoxMakeHeaderValueFn
The public wrapper type for MakeHeaderValueFn.
MakeHeaderValueDefault
Marker type to allow types which are MakeHeaderValue and also have a Default way to construct to let them be constructed on the fly. Useful alternative for cloning or using a function.
SetRequestHeader
Middleware that sets a header on the request.
SetRequestHeaderLayer
Layer that applies SetRequestHeader which adds a request header.
TypedHeaderAsMaker
Wrapper used internally as part of making typed headers encode header values on the spot, when needed.

Traits§

MakeHeaderValue
Trait for producing header values.
MakeHeaderValueFn
Functional version of MakeHeaderValue.