我正在使用 Golang 1.4.2(从源代码构建),当我尝试通过 http.Client.Do() 发出 HTTP PUT 请求时,请求中缺少 Content-Length 标头。我所有的其他标题都被发送了......我做错了什么吗?当我通过 CURL 发出相同的请求时,将发送内容长度标头。我的请求是向 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