从golang中的GET请求中获取状态码

huangapple go评论83阅读模式
英文:

Getting the status code from a get request in golang

问题

我正在尝试在Goland中获取HTTP状态码。

我也传递了授权令牌。

这是我到目前为止尝试的代码:

func StatusCode(PAGE string, AUTH string) (r string){

	resp, err := http.NewRequest("GET", PAGE, nil)
    if err != nil {
        log.Fatal(err)
    }
    resp.Header.Set("Authorization", AUTH)

    fmt.Println("HTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))

    r := resp.StatusCode + http.StatusText(resp.StatusCode)
}

基本上,我想要获取这个:

r = "200 OK"
或者
r = "400 Bad request"

之前的代码中有问题,它在resp.StatusCodehttp.StatusText(resp.StatusCode)处报错。

英文:

I'm trying to get the http status code in goland.

I'm passing the authorization token as well.

This is what I tried so far:

func StatusCode(PAGE string, AUTH string) (r string){

	resp, err := http.NewRequest("GET", PAGE, nil)
    if err != nil {
        log.Fatal(err)
    }
    resp.Header.Set("Authorization", AUTH)

    fmt.Println("HTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))

    r := resp.StatusCode + http.StatusText(resp.StatusCode)
}

Basically I want to get this:

r = "200 OK"
or
r= "400 Bad request"

The previous code it´s complaining from resp.StatusCode and http.StatusText(resp.StatusCode)

答案1

得分: 3

有两个问题。第一个问题是应用程序将请求用作响应。执行请求以获取响应。

第二个问题是resp.StatusCode + http.StatusText(resp.StatusCode)无法编译,因为操作数类型不匹配。resp.StatusCode的值是int类型。http.StatusText(resp.StatusCode)的值是string类型。Go语言没有将数字隐式转换为字符串的功能,这样无法按照你的期望工作。

如果你想要获取服务器发送的状态字符串,可以使用r := resp.Status

如果你想要构建一个由服务器状态码和Go语言状态字符串组成的状态字符串,可以使用r := fmt.Sprintf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode))

以下是代码:

func StatusCode(PAGE string, AUTH string) (r string) {
    // 设置请求。
    req, err := http.NewRequest("GET", PAGE, nil)
    if err != nil {
        log.Fatal(err)
    }
    req.Header.Set("Authorization", AUTH)

    // 执行请求。
    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        return err.Error()
    }
    
    // 根据需要关闭响应体。
    defer resp.Body.Close()

    fmt.Println("HTTP Response Status:", resp.StatusCode, http.StatusText(resp.StatusCode))

    return resp.Status
    // 或者使用 fmt.Sprintf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}

以上是翻译好的内容,请确认是否满意。

英文:

There are two problems. The first is that the application uses the request as the response. Execute the request to get the response.

The second problem is that resp.StatusCode + http.StatusText(resp.StatusCode) does not compile because operand types are mismatched. The value resp.StatusCode is an int. The value of http.StatusText(resp.StatusCode) is a string. Go does not have the implicit conversion of numbers to strings that would make this work the way you expect.

Use r := resp.Status if you want the status string as sent from the server.

Use r := fmt.Sprintf("%d %s", resp.StatusCode, http.StatusText(resp.StatusCode)) to construct a status string from the server's status code and the Go's status strings.

Here's the code:

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))
}

huangapple
  • 本文由 发表于 2022年6月14日 02:46:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/72607685.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定