Aus*_*gen 4 http put go content-length
我正在使用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))
}
Run Code Online (Sandbox Code Playgroud)
产量
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
Run Code Online (Sandbox Code Playgroud)
关于Content-Length没有被发送我是不正确的,我只是在使用httputil.DumpRequest时没有看到它.
这里的解决方案是使用httputil.DumpRequestOut,它正确显示了Content-Length标头(以及其他标头).这意味着我的程序还有其他事情导致etcd设置空值.如果我想到这一点,我也将使用该解决方案进行更新.
| 归档时间: |
|
| 查看次数: |
8615 次 |
| 最近记录: |