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 bind url params #1337

Closed
3 tasks done
abaird1992 opened this issue May 14, 2019 · 2 comments
Closed
3 tasks done

Unable to bind url params #1337

abaird1992 opened this issue May 14, 2019 · 2 comments
Assignees
Labels

Comments

@abaird1992
Copy link

abaird1992 commented May 14, 2019

/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
/ / / / / / / / / / / / / / / / / /

Please use forum https://forum.labstack.com to ask questions!

/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
/ / / / / / / / / / / / / / / / / /

Issue Description

When I use the Context#Bind method of the Context#DefaultBinder (default implementation for the Context#Binder interface), I cannot bind the parameters contained in the url/path of the request.

Checklist

  • Dependencies installed
  • No typos
  • Searched existing issues and docs

Expected behavior

Code for the handler (derived from the examples in the documentation):

e.GET("/users/:name", func(c echo.Context) error {
  u := new(User)
  if err = c.Bind(u); err != nil {
    return
  }
  return c.JSON(http.StatusOK, u)
})

Code defining user type, also taken from the examples in the documentation

// User
type User struct {
  Name  string `json:"name" form:"name" url:"name"`
  Email string `json:"email" form:"email" query:"email"`
}

If I run the following request:

curl -XGET http://localhost:1323/users/Joe\?email\=joe_email

I would expect the Name field in u to be equal to "Joe".

Actual behavior

When executing the following request:

curl -XGET http://localhost:1323/users/Joe\?email\=joe_email

The Name field in u remains empty.

Steps to reproduce

Working code to debug

Version/commit

v4.1.5

@saka-shin
Copy link

saka-shin commented Jun 11, 2019

The path parameter should be retrieved via echo.Context#Param(string) string
https://echo.labstack.com/guide/request
Please refer to the path parameter section.

So, your code will be like following.

e.GET("/users/:name", func(c echo.Context) error {
  u := new(User)
  u.Name = c.Param("name")
  if err = c.Bind(u); err != nil {
    return
  }
  return c.JSON(http.StatusOK, u)
})

@vishr
Copy link
Member

vishr commented Jun 13, 2019

We don't support binding path parameter, this is something for v5. #1302

@vishr vishr closed this as completed Jun 13, 2019
@vishr vishr self-assigned this Jun 13, 2019
@vishr vishr added the v5 label Jun 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants