Struct axum::extract::OriginalUri
source · pub struct OriginalUri(pub Uri);
Available 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§
source§impl Clone for OriginalUri
impl Clone for OriginalUri
source§fn clone(&self) -> OriginalUri
fn clone(&self) -> OriginalUri
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for OriginalUri
impl Debug for OriginalUri
source§impl<S> FromRequestParts<S> for OriginalUriwhere
S: Send + Sync,
impl<S> FromRequestParts<S> for OriginalUriwhere S: Send + Sync,
§type Rejection = Infallible
type Rejection = Infallible
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.
Auto Trait Implementations§
impl RefUnwindSafe for OriginalUri
impl Send for OriginalUri
impl Sync for OriginalUri
impl Unpin for OriginalUri
impl UnwindSafe for OriginalUri
Blanket Implementations§
source§impl<S, B, T> FromRequest<S, B, ViaParts> for Twhere
B: Send + 'static,
S: Send + Sync,
T: FromRequestParts<S>,
impl<S, B, T> FromRequest<S, B, ViaParts> for Twhere B: Send + 'static, S: Send + Sync, T: FromRequestParts<S>,
§type Rejection = <T as FromRequestParts<S>>::Rejection
type Rejection = <T as FromRequestParts<S>>::Rejection
If the extractor fails it’ll use this “rejection” type. A rejection is
a kind of error that can be converted into a response.