Axum Uri Doesn't Giving Full Path in Nested Routers

Hi,

I'm building a Axum web app. I wrote a middleware to parse url but I can't get uri correctly.

As you can see tracing can see url fully but when I print request I only get some part of url.

Expected: /admin/users/2
What I have: /2

Can you please provide the source code that produced above output? Also please prefer to post text as such instead of an image of it.

1 Like

Sorry for image here is terminal output:

2025-01-29T11:29:11.667256Z DEBUG request{method=PATCH uri=/admin/users/2 version=HTTP/1.1}: tower_http::trace::on_request: started processing request
Request {
    method: PATCH,
    uri: /2,
    version: HTTP/1.1,
    headers: {
        "host": "localhost:2344",
        "connection": "keep-alive",
        "content-type": "application/json",
        "authorization": "bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE3MzgxNDkxNTksImV4cCI6MTczODE1MDA1OSwibmJmIjoxNzM4MTQ5MTU5LCJ1c2VyX2lkIjowfQ.1m_8assJCHlmeT1ZcBOHy0Sz7bOhXpPsCz44UADhDxM",
        "user-agent": "Restfox/0.35.0",
        "accept": "*/*",
        "accept-language": "*",
        "sec-fetch-mode": "cors",
        "accept-encoding": "gzip, deflate",
        "content-length": "121",
    },
    body: Body(
        UnsyncBoxBody,
    ),
}

Sharing source code is a bit complex but problem is about nesting. I nest couple of routings in admin then some others in admin/user. I did some tests and problem is in nest.

Let me change question. How can get full uri in nested routers.

Thanks. Can you then please provide a MRE?

Try using OriginalUri.

3 Likes

@jofas Already answered my question but in case someone need to understand details here is what I'm saying.

I think it's a minimal example. Printing in middle shows only {user_id} let's say a number but expected result /users/some_number

#[tokio::main]
async fn main() {
    let router = Router::new().nest("/users", nest());
    let listener = TcpListener::bind("127.0.0.1:5555").await.unwrap();

    axum::serve(listener, router).await.unwrap()
}

fn nest() -> Router {
    Router::new().route(
        "/{user_id}",
        get(handler).route_layer(axum::middleware::from_fn(middle)),
    )
}

async fn handler() -> impl IntoResponse {
    "Hey"
}

pub async fn middle(request: Request, next: Next) -> impl IntoResponse {
    println!("{}", request.uri());
    next.run(request).await;
}

Thanks this is what I need.

1 Like