bandian
V2EX  ›  问与答

golang 的 net/http 包支持全局添加统一的 header 吗

  •  
  •   bandian · Oct 11, 2020 · 3110 views
    This topic created in 2228 days ago, the information mentioned may be changed or developed.

    之前项目中有很多接口的请求都是用 golang 的 net/http 来发送的各种请求,但是现在需要在这些请求的 header 里加一些字段,不知道有没有什么方法可以在全局的层面给使用 net/http 进行的请求统一添加 header 字段

    9 replies  •  2020-10-15 01:01:18 +08:00
    Muninn
    Muninn
    Oct 11, 2020
    1
    自己去看文档啊……这有啥问的
    bandian
    bandian
    OP
    Oct 11, 2020
    2
    @Muninn 因为没找到啊
    FutherAll
    FutherAll
    Oct 11, 2020 via iPhone
    3
    用 middleware ?
    QingXuJiaZhi
    QingXuJiaZhi
    Oct 11, 2020
    4
    可以通过 middleware 的方式,看这里 https://gowebexamples.com/basic-middleware/
    catcn
    catcn
    Oct 11, 2020
    6
    看源码啊
    xuebing
    xuebing
    Oct 12, 2020
    7
    给 Client 加的话,用 Transport,自实现 RoundTripper
    elissa2333
    elissa2333
    Oct 15, 2020
    8
    @cmdOptionKana 别人用的 client 不是 server
    elissa2333
    elissa2333
    Oct 15, 2020
    9
    自己在外边再封住一层就好了
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4586 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 06:46 · PVG 14:46 · LAX 22:46 · JFK 01:46
    ♥ Do have faith in what you're doing.