Skip to content

echo.Context.Bind() not work in a simple demo #2055

Answered by aldas
f5i23q999d asked this question in Q&A
Discussion options

You must be logged in to vote

please add code as text form.

type User struct {
	Name  string `json:"name"`
	Email string `json:"email"`
}

func main() {
	e := echo.New()

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

	log.Fatal(e.Start(":8080"))
}
curl -v -H 'Content-Type: application/json' -d '{"name":"Joe","email":"joe@labstack"}' http://localhost:8080/users

Output:

x@x:~/code/$ curl -v -H 'Content-Type: application/json' \
  -d '{"name":"Joe","email":"joe@labstack"}' \
  http://localhost:8080/users
*   Trying 127.0.0.1:8080...
* Connected to localhost (127.0.0.1) port 8080 (#0)
> POST /users HTTP…

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@f5i23q999d
Comment options

@aldas
Comment options

@f5i23q999d
Comment options

@aldas
Comment options

Answer selected by f5i23q999d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants