【发布时间】:2015-08-06 08:58:26
【问题描述】:
我正在使用 Golang 1.4.2(从源代码构建),当我尝试通过 http.Client.Do() 发出 HTTP PUT 请求时,请求中缺少 Content-Length 标头。我所有的其他标头都已发送...我做错了吗?当我通过 CURL 发出相同的请求时,会发送 content-length 标头。我的请求正在向 etcd 服务器发出,它将我的所有键设置为空值。虽然这有点新奇,但几乎没有用处。 :)
http://play.golang.org/p/pIoB--bXUT
package main
import (
"bytes"
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"strconv"
)
func main() {
put := url.Values{}
put.Set("value", "WHOAH here is my stuff")
put.Add("ttl","")
encode := put.Encode()
req, _ := http.NewRequest("PUT", "http://localhost:2379/v2/keys/somekey", bytes.NewBufferString(encode))
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("Content-Length", strconv.Itoa(len(encode)))
req.Header.Add("X-Content-Length", strconv.Itoa(len(encode)))
dump, _ := httputil.DumpRequest(req, true)
fmt.Println(string(dump))
}
产量
PUT /v2/keys/somekey HTTP/1.1
Host: localhost
Content-Type: application/x-www-form-urlencoded
X-Content-Length: 33
ttl=&value=WHOAH+here+is+my+stuff
【问题讨论】:
-
ehrm...您转储的请求似乎设置了
Content-Type标头。 -
代码正在运行。不是问题。
-
鉴于问题的其余部分,我怀疑他的意思是
Content-Length标头:这是输出中未包含的标头。 -
如果缺少内容长度,那么您需要 DumpRequestOut,因为该标头不是您可以覆盖的标头,并且由 Transport 设置。
-
@JimB DumpRequestOut 是我所需要的。谢谢!
标签: http go put content-length