You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[ x ] I have looked for existing issues (including closed) about this
Bug Report
Platform
Darwin Ahmeds-MacBook-Air.local 24.2.0 Darwin Kernel Version 24.2.0: Fri Dec 6 18:51:28 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T8112 arm64
Description
I tried for a long time to get this to work, and I'm fairly confident I'm misunderstanding how this extractor is meant to be used and/or the expected result. Anyways, the issue is that with the following code, theOption<Path<T>> extractor doesn't actually ever return the None variant.
use axum::{extract::Path, routing::get,Router};use tokio::net::TcpListener;#[tokio::main]asyncfnmain(){let app = Router::new().route("/{name}",get(handler));let listener = TcpListener::bind("localhost:8080").await.unwrap();
axum::serve(listener, app).await.unwrap();}asyncfnhandler(name:Option<Path<String>>) -> String{match name {Some(Path(name)) => format!("{name}\n"),None => "hello\n".to_string(),}}
Here are some snippets from my command-line: No path - I expect this to error
$ curl -i http://localhost:8080
HTTP/1.1 404 Not Found
content-length: 0
date: Sun, 02 Feb 2025 04:37:43 GMT
Just a slash - I expect this to show hello\n, but instead it errors too
$ curl -i http://localhost:8080/
HTTP/1.1 404 Not Found
content-length: 0
date: Sun, 02 Feb 2025 04:37:43 GMT
A path parameter - This works
$ curl -i http://localhost:8080/some-random-string
HTTP/1.1 200 OK
content-type: text/plain; charset=utf-8
content-length: 19
date: Sun, 02 Feb 2025 04:43:03 GMT
some-random-string
Version
├── axum v0.8.1
│ ├── axum-core v0.5.0
The text was updated successfully, but these errors were encountered:
demhadais
changed the title
Option<Path<T>> extractor does not act as expectedOption<Path<T>> extractor does not return NoneFeb 2, 2025
We should probably improve the documentation for this. Option<Path<T>> is for when you wsnt to have multiple routes that use the same handler function, where one has parameters and the other one not. Since you only have one route in your example, you always get the same behavior. Try adding .route("/", get(handler)).
Bug Report
Platform
Darwin Ahmeds-MacBook-Air.local 24.2.0 Darwin Kernel Version 24.2.0: Fri Dec 6 18:51:28 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T8112 arm64
Description
I tried for a long time to get this to work, and I'm fairly confident I'm misunderstanding how this extractor is meant to be used and/or the expected result. Anyways, the issue is that with the following code, the
Option<Path<T>>
extractor doesn't actually ever return theNone
variant.Here are some snippets from my command-line:
No path - I expect this to error
Just a slash - I expect this to show
hello\n
, but instead it errors tooA path parameter - This works
Version
The text was updated successfully, but these errors were encountered: