echo.Context.Bind() not work in a simple demo #2055
Answered
by
aldas
f5i23q999d
asked this question in
Q&A
-
Beta Was this translation helpful? Give feedback.
Answered by
aldas
Dec 28, 2021
Replies: 1 comment 4 replies
-
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/1.1
> Host: localhost:8080
> User-Agent: curl/7.74.0
> Accept: */*
> Content-Type: application/json
> Content-Length: 37
>
* upload completely sent off: 37 out of 37 bytes
* Mark bundle as not supporting multiuse
< HTTP/1.1 201 Created
< Content-Type: application/json; charset=UTF-8
< Date: Tue, 28 Dec 2021 14:18:36 GMT
< Content-Length: 38
<
{"name":"Joe","email":"joe@labstack"}
* Connection #0 to host localhost left intact |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
f5i23q999d
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
please add code as text form.
Output: