Content-Length header 未在使用 PUT 方法的 Golang http 客户端请求中设置
Content-Length header is not set in Golang http client request using PUT method
我正在使用 Golang 1.4.2(从源代码构建),当我尝试通过 http.Client.Do() 发出 HTTP PUT 请求时,Content-Length header 丢失了要求。我所有其他 header 都已发送...我做错了什么吗?当我通过 CURL 发出相同的请求时,content-length header 被发送。我的请求正在向 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
如果你说 header Content-Length
没有设置(实际上它是自动设置的,只是在转储时没有显示),它按设计工作,因为下面的 headers被排除在 httputil.DumpRequest()
中,即使您明确设置它们也是如此:
Host
Content-Length
Transfer-Encoding
Trailer
参见 go/src/net/http/httputil/dump.go
的 line 317。
如果您发送请求而不是转储它,您将看到 header Content-Length
与 User-Agent
和 Accept-Encoding
.
一起发送
关于 Content-Length 没有被发送我是不正确的,我只是在使用 httputil.DumpRequest 时没有看到它。
此处的解决方案是使用 httputil.DumpRequestOut,它可以正确显示 Content-Length header(以及其他)。这意味着我的程序正在发生其他事情导致 etcd 设置空值。如果我弄明白了,我也会更新该解决方案。
我正在使用 Golang 1.4.2(从源代码构建),当我尝试通过 http.Client.Do() 发出 HTTP PUT 请求时,Content-Length header 丢失了要求。我所有其他 header 都已发送...我做错了什么吗?当我通过 CURL 发出相同的请求时,content-length header 被发送。我的请求正在向 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
如果你说 header Content-Length
没有设置(实际上它是自动设置的,只是在转储时没有显示),它按设计工作,因为下面的 headers被排除在 httputil.DumpRequest()
中,即使您明确设置它们也是如此:
Host
Content-Length
Transfer-Encoding
Trailer
参见 go/src/net/http/httputil/dump.go
的 line 317。
如果您发送请求而不是转储它,您将看到 header Content-Length
与 User-Agent
和 Accept-Encoding
.
关于 Content-Length 没有被发送我是不正确的,我只是在使用 httputil.DumpRequest 时没有看到它。
此处的解决方案是使用 httputil.DumpRequestOut,它可以正确显示 Content-Length header(以及其他)。这意味着我的程序正在发生其他事情导致 etcd 设置空值。如果我弄明白了,我也会更新该解决方案。