-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Closed
Labels
Description
The following program prints:
got err: <nil>
got body: "the bar is closed\n"
got err: write tcp 127.0.0.1:8080: connection reset by peer
I think the second error is a bug - the http package
isn't waiting for the response before printing the
error from writing to the server.
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"strings"
)
func main() {
http.HandleFunc("/bar", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "the bar is closed", 404)
})
go http.ListenAndServe(":8080", nil)
post(strings.NewReader("hello"))
post(bytes.NewBuffer(make([]byte, 4e6)))
}
func post(r io.Reader) {
resp, err := http.Post("http://localhost:8080/bar";, "text/utf8", r)
fmt.Printf("got err: %v\n", err)
if resp != nil {
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
fmt.Printf("got body: %q\n", data)
}
}