I use go-chi as HTTP-router, and I want to reuse one method inside another
func Registration(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body) // if you delete this line, the user will be created
// ...other code
// if all good then create new user
user.Create(w, r)
}
...
func Create(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
// ...other code
// ... there I get the problem with parse JSON from &b
}
user.Create
return error "unexpected end of JSON input"
Actually, after I executed ioutil.ReadAll
user.Create
ceased to parse JSON,
in r.Body
there was an empty array[]
how can I fix this problem?
The outer handler reads the request body to EOF. When the inner handler is called, there's nothing more to read from the body.
To fix the issue, restore the request body with the data read previously in the outer handler:
func Registration(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
// ...other code
r.Body = ioutil.NopCloser(bytes.NewReader(b))
user.Create(w, r)
}
The function bytes.NewReader()
returns a io.Reader
on a byte slice. The function ioutil.NopCloser
converts an io.Reader
to the io.ReadCloser
required for r.Body
.
In the end, I was able to recover data in this way:
r.Body = ioutil.NopCloser(bytes.NewBuffer(b))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With