英文:
Browser return empty response with golang when doing http.Get in rest API
问题
尝试在执行http.Get时捕获错误,但如果远程服务器出现错误,比如没有响应、DNS名称无法解析,浏览器会返回空响应。正确的做法是什么?
示例代码:
func GetStatus(w http.ResponseWriter, r *http.Request){
resp,err := http.Get("https://goodsfdsfgle.com")
if err != nil {
fmt.Fprint(w,"Remote server ok !")
} else {
fmt.Fprint(w,"Remote server error")
}
}
func handleRequests() {
http.HandleFunc("/status", GetStatus)
log.Fatal(http.ListenAndServe(":10000",nil))
}
func main() {
handleRequests()
}
当在浏览器中打开localhost:10000/status时,会得到以下错误:
localhost didn’t send any data. ERR_EMPTY_RESPONSE
但如果URL正确,一切正常,尝试使用defer和其他方式捕获错误,但没有起作用,仍然返回空响应。
请帮忙解决。
英文:
Trying catch error when doing http.Get but if remote server has error like no answer, DNS name not resolved it returns empty response in browser. What a right for way doing that?
Example:
func GetStatus(w http.ResponseWriter, r *http.Request){
resp,err := http.Get("https://goodsfdsfgle.com")
if err != nil {
fmt.Fprint(w,"Remote server ok !")
} else {
fmt.Fprint(w,"Remote server error")
}
}
func handleRequests() {
http.HandleFunc("/status", GetStatus)
log.Fatal(http.ListenAndServe(":10000",nil))
}
func main() {
handleRequests()
}
And when open browser localhost:10000/status get :
localhost didn’t send any data. ERR_EMPTY_RESPONSE
But if url ok , everything ok trying use defer , few kind of catch error. Not working. Give empty response
Please help
答案1
得分: 0
GetStatus
函数中的错误检查是不正确的,并且你引入了一个未使用的局部变量。请按照以下方式修复代码:
func GetStatus(w http.ResponseWriter, r *http.Request) {
_, err := http.Get("https://goodsfdsfgle.com")
if err != nil {
fmt.Fprint(w, "远程服务器错误")
} else {
fmt.Fprint(w, "远程服务器正常!")
}
}
尝试使用curl localhost:10000/status
在本地访问该服务。你应该会收到其中一个远程服务器...
的消息。
你需要进行更多的调整才能创建一个可工作的HTTP服务,但是请先尝试上述代码以确保最初的工作。
英文:
The error check in the GetStatus
function is incorrect and you introduced a local variable that you are not using. Please fix the code as follows:
func GetStatus(w http.ResponseWriter, r *http.Request) {
_, err := http.Get("https://goodsfdsfgle.com")
if err != nil {
fmt.Fprint(w, "Remote server error")
} else {
fmt.Fprint(w, "Remote server ok !")
}
}
Try accessing the service locally using curl localhost:10000/status
. You should receive one of the Remote server...
messages.
You will need to make more adjustments to create a working HTTP service, but please try the above code just to make things working initially.
答案2
得分: 0
package main
import (
"fmt"
"log"
"net/http"
)
func GetStatus(w http.ResponseWriter, r *http.Request) {
_, err := http.Get("https://goodsfdsfgle.com")
if err != nil {
fmt.Fprint(w, "远程服务器错误")
} else {
fmt.Fprint(w, "远程服务器正常!")
}
}
func handleRequests() {
http.HandleFunc("/status", GetStatus)
log.Fatal(http.ListenAndServe(":10000", nil))
}
func main() {
handleRequests()
}
以上是要翻译的代码部分。
英文:
package main
import (
"fmt"
"log"
"net/http"
)
func GetStatus(w http.ResponseWriter, r *http.Request){
_,err := http.Get("https://goodsfdsfgle.com")
if err != nil {
fmt.Fprint(w,"Remote server error")
} else {
fmt.Fprint(w,"Remote server ok!")
}
}
func handleRequests() {
http.HandleFunc("/status", GetStatus)
log.Fatal(http.ListenAndServe(":10000",nil))
}
func main() {
handleRequests()
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论