Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to get param value with the paths specified #2364

Closed
smaheshs opened this issue Dec 13, 2022 · 4 comments
Closed

Unable to get param value with the paths specified #2364

smaheshs opened this issue Dec 13, 2022 · 4 comments

Comments

@smaheshs
Copy link

smaheshs commented Dec 13, 2022

Hi All,

With the below configuration, I am not able to get the path param in the GET request.

import "github.com/labstack/echo/v4"

e := echo.New()

e.POST("/attributes/group/:confirm", func(c echo.Context) error { return c.NoContent(http.StatusOK) })
e.PATCH("/attributes/group/:confirm", func(c echo.Context) error { return c.NoContent(http.StatusOK) })
e.POST("/attributes/groups/:activated", func(c echo.Context) error { return c.NoContent(http.StatusOK) })
e.GET("/attributes/group/:id", func(c echo.Context) error {
	param := c.Param("id")
	log.Println("the param is", param)
	return c.NoContent(http.StatusOK)
})
e.POST("/attributes/group/reorder", func(c echo.Context) error { return c.NoContent(http.StatusOK) })
e.DELETE("/attributes/group/:id", func(c echo.Context) error { return c.NoContent(http.StatusOK) })

The log print in the GET function handler prints the param blank.

Just to check if my understanding of the routes is wrong, I tested the same in gin, and it is working properly.

For now changed the path of the GET request slightly to overcome this issue, but could someone please explain why this doesn't work.

Thanks.

@arctouch-rafaelturnes
Copy link

I think it's related to this issue 1306. It'll be changed to version 5, I guess.

The first added route will define the param's name for the rest of the related routes. If you print the params names c.ParamNames() in the route e.GET(/attributes/group/:id, you'll see that the name is confirm instead of id. Because e.POST("/attributes/group/:confirm" was defined first.

If you put the e.GET(/attributes/group/:id in the first place of all the routes, the param will be id for the rest of the related routes.

@smaheshs
Copy link
Author

Thanks @arctouch-rafaelturnes. That was indeed the issue.

Closing it since there is an ongoing discussion for the same.

@aldas
Copy link
Contributor

aldas commented Dec 14, 2022

What version you are using? I think this is resolved in v4.8.0 with this PR #2209

@smaheshs
Copy link
Author

HI @aldas, this project was created before and was using version 4.2.x. I updated now and checked in 4.9.1 and it is fixed and working as expected.

Thanks for the help.

@aldas aldas closed this as completed Dec 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants