我观察到 Content-Length 标头没有为具有空/nil 有效负载的 PATCH 请求设置。即使我们手动设置它,req.Header.Set("content-length", "0")它实际上并没有在发出的请求中设置。这种奇怪的行为(Go bug?)仅发生在 PATCH 请求中,并且仅在有效负载为空或 nil(或设置为 http.NoBody)时发生
package main
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
func main() {
url := "http://localhost:9999"
method := "PATCH"
payload := strings.NewReader("")
client := &http.Client {
}
req, err := http.NewRequest(method, url, payload)
if err != nil {
fmt.Println(err)
}
req.Header.Set("Authorization", "Bearer my-token")
req.Header.Set("Content-Length", "0") //this is not honoured
res, err := client.Do(req)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
fmt.Println(string(body))
}
即使在最新的 go 版本中,这也是可重现的1.15。只需在一个简单的 http 服务器上运行上面的代码,然后自己看看。
是否有任何解决方案/解决方法可以发送 Content-Length 设置为 0 的 PATCH 请求?
慕姐8265434
相关分类