我正在尝试使用 Go 模拟 curl -X GET,但我正在联系的服务器具有身份验证。我已经关注了几个推荐我使用的网站r.Header.Add(),但我的 curl 调用无法正常工作。我的 curl 调用实际上返回了一些东西: curl -X GET https://myserver.com/test/anothertest -H 'x-access-token: a1b2c3d4'我的代码没有返回预期的 JSON 对象:func get(api string, headers map[string]string, dataStruct interface{}) (data interface{}, err error) { req, _ := http.NewRequest("GET", api, nil) for k, v := range headers { req.Header[k] = []string{v} } currentHeader, _ := httputil.DumpRequestOut(req, true) fmt.Println(string(currentHeader)) client := &http.Client{} resp, err := client.Do(req) if err != nil { return } defer resp.Body.Close() data = dataStruct err = getJson(api, &data) if err != nil { return } return}func main() { // args := parse_args(Options{}) args := Options{Api: "my_server", Headers: map[string]string{ "x-access-token": "a1b2c3d4"}} body, _ := get(args.Api, args.Headers, HistoryDecoder{}) fmt.Println(body)}退货:GET /v1pre3/users/current HTTP/1.1Host: my_serverUser-Agent: Go-http-client/1.1X-Access-Token: a1b2c3d4Accept-Encoding: gzipmap[ResponseStatus:map[Message:Please ensure that valid credentials are being provided for this API call (This request requires authentication but none were provided)] Notifications:[map[Type:error Item:Please ensure that valid credentials are being provided for this API call (This request requires authentication but none were provided)]]]谁能告诉我我做错了什么?
1 回答
POPMUISE
TA贡献1765条经验 获得超5个赞
经过大量重写和调试后,我发现它也在getJson(api, &data)
进行http.Get 调用,但没有标头。由于那是在我们讨论的请求之后发生的,因此它绕过了所有测试和打印。
- 1 回答
- 0 关注
- 95 浏览
添加回答
举报
0/150
提交
取消