pub struct OriginalUri(pub Uri);
This is supported on crate feature original-uri only.
Expand description

Extractor that gets the original request URI regardless of nesting.

This is necessary since Uri, when used as an extractor, will have the prefix stripped if used in a nested service.

Example

use axum::{
    routing::get,
    Router,
    extract::OriginalUri,
    http::Uri
};

let api_routes = Router::new()
    .route(
        "/users",
        get(|uri: Uri, OriginalUri(original_uri): OriginalUri| async {
            // `uri` is `/users`
            // `original_uri` is `/api/users`
        }),
    );

let app = Router::new().nest("/api", api_routes);

Extracting via request extensions

OriginalUri can also be accessed from middleware via request extensions. This is useful for example with Trace to create a span that contains the full path, if your service might be nested:

use axum::{
    Router,
    extract::OriginalUri,
    http::Request,
    routing::get,
};
use tower_http::trace::TraceLayer;

let api_routes = Router::new()
    .route("/users/:id", get(|| async { /* ... */ }))
    .layer(
        TraceLayer::new_for_http().make_span_with(|req: &Request<_>| {
            let path = if let Some(path) = req.extensions().get::<OriginalUri>() {
                // This will include `/api`
                path.0.path().to_owned()
            } else {
                // The `OriginalUri` extension will always be present if using
                // `Router` unless another extractor or middleware has removed it
                req.uri().path().to_owned()
            };
            tracing::info_span!("http-request", %path)
        }),
    );

let app = Router::new().nest("/api", api_routes);

Tuple Fields

0: Uri

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

If the extractor fails it’ll use this “rejection” type. A rejection is a kind of error that can be converted into a response. Read more

Perform the extraction.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more