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 关注
- 273 浏览
添加回答
举报