意外的EOF在发送POST请求时

huangapple go评论81阅读模式
英文:

Unexpected EOF when sending POST request

问题

我在使用http包发送一个简单的POST请求时遇到了一些问题:

var http_client http.Client

req, err := http.NewRequest("POST", "http://login.blah", nil)
if err != nil {
  return errors.New("创建登录请求时出错:" + err.Error())
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
body := fmt.Sprintf("?username=%s&password=%s&version=%d", client.Username, client.Password, launcherVersion)
fmt.Println("请求体:", body)
req.Body = ioutil.NopCloser(bytes.NewBufferString(body))
req.ParseForm()
resp, err := http_client.Do(req)

if err != nil {
  return errors.New("发送登录请求时出错:" + err.Error())
}

我在打印输出中看到了正确的请求体:

请求体:?username=test&password=test&version=13

但是60秒后,我收到了以下错误信息:

发送登录请求时出错:意外的EOF

我确定问题与我设置请求体的方式有关,因为使用Wireshark观察请求时,可以看到请求立即发送出去,但Content-Length为0,没有请求体。

POST / HTTP/1.1
Host: login.blah
User-Agent: Go http package
Content-Length: 0
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip
英文:

I'm having some trouble sending a simple POST request with the http package:

var http_client http.Client

req, err := http.NewRequest("POST", "http://login.blah", nil)
if err != nil {
  return errors.New("Error creating login request: " + err.Error())
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
body := fmt.Sprintf("?username=%s&password=%s&version=%d", client.Username, client.Password, launcherVersion)
fmt.Println("Body:", body)
req.Body = ioutil.NopCloser(bytes.NewBufferString(body))
req.ParseForm()
resp, err := http_client.Do(req)

if err != nil {
  return errors.New("Error sending login request: " + err.Error())
}

I see the correct body from the print:

Body: ?username=test&password=test&version=13

But after 60 seconds, I get:

Error sending login request: unexpected EOF

I'm sure it has something to do with how I set the request body, because watching it with Wireshark shows me the request, which goes out right away, has a Content-Length of 0 with no body.

POST / HTTP/1.1
Host: login.blah
User-Agent: Go http package
Content-Length: 0
Content-Type: application/x-www-form-urlencoded
Accept-Encoding: gzip

答案1

得分: 3

你的body字符串看起来像是一个URL的结尾,就像你在发送GET请求时发送参数一样。

服务器可能期望你的POST请求的body以multipart/form-data格式发送,该格式在http://www.w3.org/TR/html401/interact/forms.html#form-data-set中定义。

我认为你应该:

resp, err := http.PostForm("http://example.com/form",
    url.Values{"key": {"Value"}, "id": {"123"}})
英文:

Your body string looks like the end of an URL, just like it would be if you were sending your parameter in a GET request.

The server probably expects the body of your POST request to be in multipart/form-data format as defined in http://www.w3.org/TR/html401/interact/forms.html#form-data-set

I think you should either

  • use a multipart.Writer to build your body.

  • use PostForm as in the package example :

     resp, err := http.PostForm("http://example.com/form",
         url.Values{"key": {"Value"}, "id": {"123"}})
    

huangapple
  • 本文由 发表于 2012年6月9日 02:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/10953933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定