Struct axum::extract::OriginalUri
source ·
[−]pub struct OriginalUri(pub Uri);
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
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. Read more
fn from_request<'life0, 'async_trait>(
req: &'life0 mut RequestParts<B>
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'life0 mut RequestParts<B>
) -> Pin<Box<dyn Future<Output = Result<Self, Self::Rejection>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
Perform the extraction.
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
Mutably borrows from an owned value. Read more
Create a new Policy
that returns Action::Follow
only if self
and other
return
Action::Follow
. Read more
pub fn vzip(self) -> V
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