将不变的小写标题添加到 Golang 的 HTTP 请求中。

huangapple go评论143阅读模式
英文:

Add unaltered lowercase headers to golang http request

问题

我正在尝试在一个golang程序中设置全部小写的标头,但是CanonicalMIMEHeaderKey会将第一个字母大写。我正在使用的API目前只接受全部小写的标头。在这个时间点上,改变这一点不是一个选项。有没有办法覆盖这个行为?

http://golang.org/pkg/net/textproto/#CanonicalMIMEHeaderKey

例如,我想要添加的标头是:

req.Header.Add("myheader", "myheadervalue")

但是它变成了:

req.Header.Add("Myheader", "myheadervalue")

有人可以帮忙吗?

谢谢。

英文:

I am trying to set all-lowercase headers in a golang program and CanonicalMIMEHeaderKey is uppercasing the first letter. The API I am consuming only takes this particular header in all-lowercase at the moment. It's not an option to change that at this point in time. Is there a way to override that?

http://golang.org/pkg/net/textproto/#CanonicalMIMEHeaderKey

So for example, the header I want to add is:

req.Header.Add("myheader", "myheadervalue")

But it comes out as:

req.Header.Add("Myheader", "myheadervalue")

Can anyone help please?

Thanks

答案1

得分: 10

我看不到绕过这个问题的方法,但如果你真的必须使用小写的标头名称,那么你可以通过创建自己的具有小写键的http.Header来解决这个问题。以下是一个示例代码(在playground上):

import "fmt"
import "strings"
import "net/http"

// ...

req, _ := http.NewRequest("GET", "http://foo", nil) 
req.Header.Add("myheader", "myheadervalue")

lowerCaseHeader := make(http.Header)

for key, value := range req.Header {
    lowerCaseHeader[strings.ToLower(key)] = value
}

req.Header = lowerCaseHeader
英文:

I do not see a way to circumvent this but if you really have to use lower-case header names, then you can work around this by creating your own http.Header with lower-case keys. Example (on play):

import "fmt"
import "strings"
import "net/http"

// ...

req, _ := http.NewRequest("GET", "http://foo", nil) 
req.Header.Add("myheader", "myheadervalue")

lowerCaseHeader := make(http.Header)

for key, value := range req.Header {
	lowerCaseHeader[strings.ToLower(key)] = value
}

req.Header = lowerCaseHeader

答案2

得分: 8

我遇到了类似的问题,并以稍微不同的方式解决了它,我认为这对于将来阅读这篇帖子的其他人可能会有帮助...

由于http.Header在内部只是一个map[string][]string,你可以创建一个http.Header,然后将其分配给请求的头部。因为这不使用Add方法或Set方法,所以这将保留大小写。

像这样:

import "net/http"

// ...

headers := http.Header{
    "my-lowercase-header": []string{"myvalue1"},
    "Accept": []string{"text/plain", "text/html"},
}

client := &http.Client{}

req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
    panic(err)
}

req.Header = headers

resp, err := client.Do(req)
英文:

I ran into a similar problem and solved it a slightly different way that I thought might be helpful for others coming to this post in the future...

Since http.Header is just a map[string][]string under the hood, you can create an http.Header then assign it to the request's header. Because this does not use the Add nor the Set method, this will preserve casing.

Like so:

import "net/http"

// ...

headers := http.Header{
    		"my-lowercase-header": []string{"myvalue1"},
    		"Accept": []string{"text/plain", "text/html"},
    	}

client := &http.Client{}

req, err := http.NewRequest("GET", "http://www.example.com", nil)
if err != nil {
    panic(err)
}

req.Header = headers

resp, err := client.Do(req)

答案3

得分: 4

有另一种简单的方法来做这件事。

req.Header["myheader"] = []string{"myheadervalue"}

英文:

There is another easy way to do it.

req.Header["myheader"] = []string{"myheadervalue"}

huangapple
  • 本文由 发表于 2014年7月25日 07:45:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/24945806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定