dongqiao8502 2014-07-24 23:45
浏览 127
已采纳

将未更改的小写标头添加到golang http请求

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

  • 写回答

3条回答 默认 最新

  • duanmei1922 2014-07-25 00:04
    关注

    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条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了