我編寫了一個(gè)簡(jiǎn)單的 go 代碼,它向 API 發(fā)送 GET 請(qǐng)求,作為響應(yīng)我收到 401 錯(cuò)誤。但是,當(dāng)我使用 cURL 時(shí),我收到了所需的響應(yīng)。我還使用API Tester獲得了預(yù)期的響應(yīng)。所以,我相信,我的代碼一定有問(wèn)題,而且我無(wú)法找出來(lái)。下面是我的 Go 代碼,它以 401 錯(cuò)誤響應(yīng)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))}我得到的響應(yīng)/錯(cuò)誤的摘錄如下:URL - https://api.qubole.com/api/v1.3/clusters/presto-bi/state{"error":{"error_code":401,"error_message":"Invalid Token"}}現(xiàn)在,以下是 cURL 命令,它返回我想要的響應(yīng)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"下面是我收到的標(biāo)準(zhǔn)輸出,這是預(yù)期的:{"state":"DOWN"}%
從 Go Code 使用 API 時(shí)出現(xiàn) 401 錯(cuò)誤,而 cURL 運(yùn)行良好
墨色風(fēng)雨
2023-05-08 17:48:55