英文:
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
- URL解析错误
- 重定向次数过多
- TCP连接、写入、读取超时
- 302状态,但Location头为空
等等
你可以在http包中阅读源代码。然后你可以找到由http.Get
函数返回的所有错误。
英文:
- URL parse error
- too much redirect times
- tcp connect\write\read timeout
- 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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论