duanping2809 2016-12-19 19:30
浏览 144
已采纳

如何在golang net / http中使用Transport添加标题信息

I am trying to control keep-alives session to reuse the tcp connection by creating a Trasport.

Here is my snippet and I am not sure how to add headers info for authentication.

url := "http://localhost:8181/api/v1/resource"
tr := &http.Transport{
    DisableKeepAlives:   false,
    MaxIdleConns:        0,
    MaxIdleConnsPerHost: 0,
    IdleConnTimeout:     time.Second * 10,
}
client := &http.Client{Transport: tr}
resp, err := client.Get(url)
  • 写回答

2条回答 默认 最新

  • dqw7121 2016-12-19 19:37
    关注

    Don't mix the Client from the Request.
    The client uses a Transport and run the request: client.Do(req)

    You set header on the http.Request with (h Header) Set(key, value string):

    req.Header.Set("name", "value")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)