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

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 多电路系统共用电源的串扰问题
  • ¥15 shape_predictor_68_face_landmarks.dat
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料