I want to make a HTTP POST request to a Django web server. My code is like:
send_data := data + data2
adata := url.Values{}
adata.Set("data", send_data)
client := &http.Client{}
req, err := http.NewRequest("POST", "http://10.26.160.124:8080/data", strings.NewReader(adata.Encode()))
if err != nil {
fmt.Println(err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err1 := client.Do(req)
if err1 != nil {
fmt.Println(err1)
}
fmt.Println(res.Status)
But the I got many errors :
{Read body failed
%!(EXTRA *errors.errorString=unexpected EOF, string=})
and
Post http://10.26.160.124:8080/data: read tcp 172.20.31.117:22301->10.26.160.124:8080: read: connection reset by peer
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7d0ef4]
goroutine 1703 [running]:
main.(*HttpTrafficHandler).handle(0xc4219013b0, 0xc4218da360)
/home/work/project/src/http_sniffer/http_traffic_handler.go:212 +0xc64
created by main.(*HttpConnectionHandler).handle
/home/work/project/src/http_sniffer/http_traffic_handler.go:56 +0x17f
and err logs on the server are all like this:
[2018-05-28 07:13:30,876] - Broken pipe from ('172.20.31.117', 23406)
[2018-05-28 07:13:30,877] - Broken pipe from ('172.20.31.117', 23158)
[2018-05-28 07:13:30,727] - Broken pipe from ('172.20.31.117', 23546)
[2018-05-28 07:13:30,879] - Broken pipe from ('172.20.31.117', 24319)
[2018-05-28 07:13:30,885] - Broken pipe from ('172.20.31.117', 23337)
So what is the problem? I am crazy!
评论:
kapoof_euw:
kriswei:One thing first, if err1 != nil, you should also halt work. You get a nilpointer because afterwards you try to access "res", which is nil because an error occurred.
Regarding "broken pipe", as far as I know that occurs when the server closes the connection while your client was still transmitting data to it. You'll need to check in your Django web server for the issue, as it's not at the side of your Go client.
Edit: I just saw that the broken pipe error is noted in the Django web server, not the Go client. That is odd, as the Go client states that the connection was reset by the other peer. Do you connect directly to your web server or is there a proxy in between (e.g. Nginx)?
drvd:It is OK now! Thank u very much!
kriswei:Cross post from https://stackoverflow.com/questions/50561182/golang-error-connection-reset-by-peer-about-go-net-http
That is my post too.lol
