我編寫了一個簡單的 go 代碼,它向 API 發送 GET 請求,作為響應我收到 401 錯誤。但是,當我使用 cURL 時,我收到了所需的響應。我還使用API Tester獲得了預期的響應。所以,我相信,我的代碼一定有問題,而且我無法找出來。下面是我的 Go 代碼,它以 401 錯誤響應func main() { clusterId := os.Getenv("CLUSTER_ID") apiUrl := "https://api.qubole.com/api/v1.3/clusters/"+clusterId+"/state" auth_token := os.Getenv("X_AUTH_TOKEN") fmt.Println("URL - ",apiUrl) req, err := http.NewRequest("GET", apiUrl, nil) if(err != nil){ fmt.Println("ERROR in getting rest response",err) } req.Header.Set("X-Auth-Token", auth_token) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") res, err := http.DefaultClient.Do(req) if(err != nil){ fmt.Println("Error: No response received", err) } defer res.Body.Close() //print raw response body for debugging purposes body, _ := ioutil.ReadAll(res.Body) fmt.Println(string(body))}我得到的響應/錯誤的摘錄如下:URL - https://api.qubole.com/api/v1.3/clusters/presto-bi/state{"error":{"error_code":401,"error_message":"Invalid Token"}}現在,以下是 cURL 命令,它返回我想要的響應curl -X GET -H "X-AUTH-TOKEN:$X_AUTH_TOKEN" -H "Content-Type: application/json" -H "Accept: application/json" "https://us.qubole.com/api/v1.3/clusters/presto-bi/state"下面是我收到的標準輸出,這是預期的:{"state":"DOWN"}%
2 回答

蕪湖不蕪
TA貢獻1796條經驗 獲得超7個贊
需要檢查 golang 的 api 主機名并再次卷曲。謝謝!
錯誤是因為,API 提供者的文檔指出主機錯誤(API 文檔)。但是,由于門戶登錄是 us.qubole.com(門戶登錄 URL),因此在編寫 cURL 命令時考慮到了這一點。

白板的微信
TA貢獻1883條經驗 獲得超3個贊
我只是在沒有足夠信息的情況下在這里猜測。我假設clusterId := os.Getenv("CLUSTER_ID")
是presto-bi
。如果是這樣,那么您只是在路徑中缺少“集群”。
apiUrl := "https://api.qubole.com/api/v1.3/clusters/"+clusterId+"/state"
另外,你不應該使用us.qubole.com/api
而不是嗎api.qubole.com
?
- 2 回答
- 0 關注
- 376 瀏覽
添加回答
舉報
0/150
提交
取消