Repository navigation
Why does Echo allow route params like /users/:id to match /users/3//////? Expected strict behavior #2890
Replies: 3 comments
|
well, there is no specification on this. Probably from start (early days of the library), the last parameter in route, if it is defined without There is remove slash middleware: https://echo.labstack.com/docs/middleware/trailing-slash#remove-trailing-slash |
|
$(cat <<'ENDOFBODY' Two ways to deal with this depending on what you want: Drop trailing slashes globally with the e := echo.New()
e.Pre(middleware.RemoveTrailingSlash())This redirects (or rewrites, depending on config) Strip it per-handler if you only care in specific places: e.GET("/users/:id", func(c echo.Context) error {
id := strings.TrimRight(c.Param("id"), "/")
// ...
})I'd go with the middleware approach — it's consistent and you don't have to remember to trim in every handler. The reason Echo doesn't normalize by default is that some APIs actually rely on trailing slashes being meaningful (REST conventions where |
|
One small thing to add to the replies above. The last If you want a hard rule, check the value in the handler: id := c.Param("id")
if id == "" || strings.Contains(id, "/") {
return echo.NewHTTPError(http.StatusBadRequest, "invalid id")
}That keeps your route table simple and makes the contract explicit at the point where you use the value. Also worth knowing: if you use the trailing slash middleware in redirect mode, a POST to |
Uh oh!
There was an error while loading. Please reload this page.
Hi Echo team 👋
First of all, thank you for the amazing framework. I’m still a Go beginner, so sorry if this is a basic question.
I’m a bit confused about route parameter behavior in Echo.
I have a route like this:
When I access:
✅ This works (expected)
But when I access:
or even:
✅ This also works, and the
idparameter becomes"3//////"(or similar).What I expected
I expected the router to be strict, where:
/users/3→ ✅ allowed/users/3/→ ❌ not allowed/users/3//////→ ❌ not allowedBecause semantically, I thought
:idshould only represent a single clean path segment, not include extra slashes.My questions
/users/3////would not match/users/:id?Context
I understand that Echo tries to be closer to raw HTTP behavior, but from an API design perspective (especially for REST APIs), strict routing feels safer and more predictable.
Again, apologies if this is a beginner misunderstanding — I’d really appreciate some clarification 🙏
Thank you!
All reactions