dtr53557 2016-10-11 03:27
浏览 81
已采纳

Golang错误的HTTP请求标头

I'm building 2 apis. One make request to another.

To call the api that receives requests, we need to pass a X-Token Header. I'm doing this with Golang

client := &http.Client{
    Transport: &http.Transport{
        TLSClientConfig: &tls.Config{
            InsecureSkipVerify: true,
        },
    },
}

req, err := http.NewRequest("GET", "https://localhost:8086/v2/example", nil)
if err != nil {
    c.JSON(http.StatusInternalServerError, gin.H{"Error": err.Error()})
}
req.Header.Add("accept", "application/json")
req.Header.Add("content-type", "application/json")
req.Header.Add("x-token", "a2e63ee01401aaeca78be023dfbb8c59")

resp, err := client.Do(req)

In the other API, i get the http header with gin like this:

token := c.Request.Header.Get("x-token")

I dont know why my header arrives with another value and no X-Token. Thanks!

Result of fmt.Printf("%+v", c.Request.Header):

map[User-Agent:[Go-http-client/1.1] Referer:[https://localhost:8086/v2/example] Accept-Encoding:[gzip]]

I don't know where is my x-token, accept and content-type headers....

IMPORTANT

  • If i make a request with x-token header on Postman to the requested API i get the right header.
  • If i change the request address on the API that makes requests, e.g httpbin, i get the right header too....
  • 写回答

1条回答 默认 最新

  • dongzou9047 2016-10-11 11:17
    关注

    Helo, guys! i found the solution....

    I don't know why yet... but i think golang don't handle no trailing slash url's....

    https://localhost:8086/v2/example
    is different of
    https://localhost:8086/v2/example/

    That was my problem....

    I just copy and past the golang generated code of postman... and that was the "biggest" difference....

    Thanks mr. Postman...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀
  • ¥15 mifare plus卡认证
  • ¥30 LSTM预测结果是一条直线
  • ¥15 stnodeeditor自定义控件
  • ¥15 SDO如何更改STM32的波特率
  • ¥15 uniapp的uni-datetime-picker组件在ios端不适配
  • ¥15 前端 uniapp App端在离线状态如何使用modbus 连接手机蓝牙进行读写操控机器?