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 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)