我正在嘗試使用 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 關注
- 139 瀏覽
添加回答
舉報
0/150
提交
取消