亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

curl 請求中未使用的標頭

curl 請求中未使用的標頭

Go
慕容708150 2023-05-04 17:08:09
我正在嘗試使用 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 調用,但沒有標頭。由于那是在我們討論的請求之后發生的,因此它繞過了所有測試和打印。



查看完整回答
反對 回復 2023-05-04
  • 1 回答
  • 0 關注
  • 139 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號