没有为有效载荷为空/无的PATCH请求设置Content-Length标头-GoLang

问题描述

我观察到没有为有效载荷为空/无的PATCH请求设置Content-Length标头。即使我们通过req.Header.Set("content-length","0")手动设置它,实际上也不在外发请求中设置它。 仅当PATCH请求且仅当有效载荷为空或为零(或设置为http.NoBody)时才会发生这种奇怪的行为(Go bug?)

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请求?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)