Getting strange 405 when a route is associated with multiple methods #3117
-
Expected BehaviorIt should accept POST, DELETE and PUT and route the request correctly Current BehaviorReturns 405, and the accept header contains the actual method used (but if invoked with POST or PUT, it's just the GET?) Steps to Reproduce (for bugs)async fn hello() -> impl Responder {
info!("Sending a String.");
"Hallo Welt"
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.service(web::resource("/hello").route(web::post().method(http::Method::DELETE).method(http::Method::PUT).to(hello)))
.default_service(
web::route().to(async || HttpResponse::NotFound().finish()),
)
})
.bind("127.0.0.1:8080")?
.run()
.await
} $ curl -i localhost:8080/hello
HTTP/1.1 405 Method Not Allowed
content-length: 0
allow: POST
date: Mon, 21 Aug 2023 13:26:25 GMT
$ curl -i localhost:8080/hello -XPOST
HTTP/1.1 405 Method Not Allowed
content-length: 0
allow: POST, DELETE
date: Mon, 21 Aug 2023 13:26:28 GMT
$ curl -i localhost:8080/hello -XDELETE
HTTP/1.1 405 Method Not Allowed
content-length: 0
allow: POST
date: Mon, 21 Aug 2023 13:26:32 GMT
$ curl -i localhost:8080/hello -XPUT
HTTP/1.1 405 Method Not Allowed
content-length: 0
allow: POST
date: Mon, 21 Aug 2023 13:26:35 GMT
ContextTrying to route multiple methods to a route Your Environment
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
That isn't how guards work. Please take a look at the guard docs, it seems that you probably want the |
Beta Was this translation helpful? Give feedback.
-
If you are okay to use macro routing, it's slightly easier to achieve: https://docs.rs/actix-web/latest/actix_web/attr.route.html #[route("/test", method = "GET", method = "HEAD", method = "CUSTOM")]
async fn example() -> HttpResponse {
HttpResponse::Ok().finish()
} |
Beta Was this translation helpful? Give feedback.
That isn't how guards work. Please take a look at the guard docs, it seems that you probably want the
Any
guard.