可能的Golang HTTP响应错误有哪些?

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

What are possible golang http response errors?

问题

在许多关于golang net/http的文章中,一个请求会返回两个值:响应和错误:

resp, err := http.Get("http://example.com/")
if err != nil {
    // 处理错误
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

对于与http相关的错误,它们将在resp中返回,带有类似502、400等的状态码。可能返回的错误有哪些?在我处理它们之前,我需要了解它们。

英文:

As in many golang net/http articles, a request returns two values: response and error:

resp, err := http.Get("http://example.com/")
if err != nil {
	// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)

For http-related errors, it will be returned in resp with status code like 502, 400 etc. What are the possible errors returned? I need to know them before I can handle them.

答案1

得分: 7

  1. URL解析错误
  2. 重定向次数过多
  3. TCP连接、写入、读取超时
  4. 302状态,但Location头为空

等等

你可以在http包中阅读源代码。然后你可以找到由http.Get函数返回的所有错误。

英文:
  1. URL parse error
  2. too much redirect times
  3. tcp connect\write\read timeout
  4. 302 status, but null Location header

and so on

You can read the source code in http package. Then you can find all the errors returned by this function http.Get.

答案2

得分: 0

这些错误大多与网络相关,比如网络超时等。没有必要对它们进行特殊处理。你可以像下面这样优雅地退出:

resp, err := http.Get("http://google.com/")
if err != nil {
    fmt.Println(err.Error())
    os.Exit(1)
}
英文:

Those errors will mostly network related errors, like network timeouts etc. There is no need to handle them differently. You may exit gracefully like,

resp, err := http.Get("http://google.com/")
	if err != nil {
	    fmt.Println(err.Error())
        os.Exit(1)
}

答案3

得分: -1

有很多错误响应,例如:

301(永久移动)
302(找到)
303(查看其他)
307(临时重定向)

最好你去阅读http.Get

英文:

there are many error responses like :-

301 (Moved Permanently)
302 (Found)
303 (See Other)
307 (Temporary Redirect) 

better you go and read http.Get

huangapple
  • 本文由 发表于 2016年11月29日 11:09:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/40857132.html
匿名

发表评论

匿名网友

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

确定