I am using github.com/pressly/chi to build this simple program where I try to decode some JSON from the http.Request.Body
:
package main
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pressly/chi"
"github.com/pressly/chi/render"
)
type Test struct {
Name string `json:"name"`
}
func (p *Test) Bind(r *http.Request) error {
err := json.NewDecoder(r.Body).Decode(p)
if err != nil {
return err
}
return nil
}
func main() {
r := chi.NewRouter()
r.Post("/products", func(w http.ResponseWriter, r *http.Request) {
var p Test
// err := render.Bind(r, &p)
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
panic(err)
}
fmt.Println(p)
})
http.ListenAndServe(":8080", r)
}
When I don't use render.Bind()
(from "github.com/pressly/chi/render"
), it works as expected.
However, when I uncomment the line err := render.Bind(r, &p)
and I comment the line err := json.NewDecoder(r.Body).Decode(&p)
, it panics with EOF
:
2017/06/20 22:26:39 http: panic serving 127.0.0.1:39696: EOF
and thus the json.Decode()
fails.
Am I doing something wrong or is the http.Request.Body
is already read somewhere else before render.Bind()
is called?