<p>I want to make a HTTP POST request to a Django web server. My code is like:</p>
<pre><code> 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)
</code></pre>
<p>But the I got many errors :</p>
<pre><code>{Read body failed
%!(EXTRA *errors.errorString=unexpected EOF, string=})
</code></pre>
<p>and </p>
<pre><code>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
</code></pre>
<p>and err logs on the server are all like this:</p>
<pre><code>[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)
</code></pre>
<p>So what is the problem? I am crazy!</p>
<hr/>**评论:**<br/><br/>kapoof_euw: <pre><p>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.</p>
<p>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.</p>
<p>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)?</p></pre>kriswei: <pre><p>It is OK now! Thank u very much! </p></pre>drvd: <pre><p>Cross post from <a href="https://stackoverflow.com/questions/50561182/golang-error-connection-reset-by-peer-about-go-net-http" rel="nofollow">https://stackoverflow.com/questions/50561182/golang-error-connection-reset-by-peer-about-go-net-http</a></p></pre>kriswei: <pre><p>That is my post too.lol</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传