I have problems with a runtime error which occurs when testing my http handles.
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
)
func main() {
rec := httptest.NewRecorder()
req, err := http.NewRequest("POST", "/", nil)
if err != nil {
log.Fatal(err)
}
req.Write(bytes.NewBufferString("Hello"))
Handle(rec, req)
}
func Handle(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Body: %s", body)
}
I read something that this occurs because of concurrent io operations on the request body. However I do not know how to solve this. Any suggestions?
PS: passing the io.Reader directly to NewRequest is not an option as I need to add headers in the original use case.