doucanshou6998 2012-06-08 18:11
浏览 2471
已采纳

发送POST请求时出现意外的EOF

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条回答 默认 最新

  • douyou1901 2012-06-08 19:55
    关注

    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"}})
      
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办