1 回答

TA貢獻1862條經驗 獲得超7個贊
有兩個問題。第一個是應用程序使用請求作為響應。 執行請求以獲得響應。
第二個問題是resp.StatusCode + http.StatusText(resp.StatusCode)
編譯不通過,因為操作數類型不匹配。該值resp.StatusCode
是一個int
. 的值為http.StatusText(resp.StatusCode)
a string
。Go 沒有將數字隱式轉換為字符串的功能,這會使它按照您期望的方式工作。
r := resp.Status
如果您想要從服務器發送的狀態字符串,請使用。
用于r := fmt.Sprintf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
從服務器的狀態代碼和 Go 的狀態字符串構造狀態字符串。
這是代碼:
func StatusCode(PAGE string, AUTH string) (r string) {
// Setup the request.
req, err := http.NewRequest("GET", PAGE, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Authorization", AUTH)
// Execute the request.
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err.Error()
}
// Close response body as required.
defer resp.Body.Close()
fmt.Println("HTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))
return resp.Status
// or fmt.Sprintf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
- 1 回答
- 0 關注
- 274 瀏覽
添加回答
舉報