浏览器在使用golang进行http.Get请求时返回空响应的问题

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

Browser return empty response with golang when doing http.Get in rest API

问题

尝试在执行http.Get时捕获错误,但如果远程服务器出现错误,比如没有响应、DNS名称无法解析,浏览器会返回空响应。正确的做法是什么?

示例代码:

  1. func GetStatus(w http.ResponseWriter, r *http.Request){
  2. resp,err := http.Get("https://goodsfdsfgle.com")
  3. if err != nil {
  4. fmt.Fprint(w,"Remote server ok !")
  5. } else {
  6. fmt.Fprint(w,"Remote server error")
  7. }
  8. }
  9. func handleRequests() {
  10. http.HandleFunc("/status", GetStatus)
  11. log.Fatal(http.ListenAndServe(":10000",nil))
  12. }
  13. func main() {
  14. handleRequests()
  15. }

当在浏览器中打开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:

  1. func GetStatus(w http.ResponseWriter, r *http.Request){
  2. resp,err := http.Get("https://goodsfdsfgle.com")
  3. if err != nil {
  4. fmt.Fprint(w,"Remote server ok !")
  5. } else {
  6. fmt.Fprint(w,"Remote server error")
  7. }
  8. }
  9. func handleRequests() {
  10. http.HandleFunc("/status", GetStatus)
  11. log.Fatal(http.ListenAndServe(":10000",nil))
  12. }
  13. func main() {
  14. handleRequests()
  15. }

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函数中的错误检查是不正确的,并且你引入了一个未使用的局部变量。请按照以下方式修复代码:

  1. func GetStatus(w http.ResponseWriter, r *http.Request) {
  2. _, err := http.Get("https://goodsfdsfgle.com")
  3. if err != nil {
  4. fmt.Fprint(w, "远程服务器错误")
  5. } else {
  6. fmt.Fprint(w, "远程服务器正常!")
  7. }
  8. }

尝试使用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:

  1. func GetStatus(w http.ResponseWriter, r *http.Request) {
  2. _, err := http.Get("https://goodsfdsfgle.com")
  3. if err != nil {
  4. fmt.Fprint(w, "Remote server error")
  5. } else {
  6. fmt.Fprint(w, "Remote server ok !")
  7. }
  8. }

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

  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. )
  7. func GetStatus(w http.ResponseWriter, r *http.Request) {
  8. _, err := http.Get("https://goodsfdsfgle.com")
  9. if err != nil {
  10. fmt.Fprint(w, "远程服务器错误")
  11. } else {
  12. fmt.Fprint(w, "远程服务器正常!")
  13. }
  14. }
  15. func handleRequests() {
  16. http.HandleFunc("/status", GetStatus)
  17. log.Fatal(http.ListenAndServe(":10000", nil))
  18. }
  19. func main() {
  20. handleRequests()
  21. }

以上是要翻译的代码部分。

英文:
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. )
  7. func GetStatus(w http.ResponseWriter, r *http.Request){
  8. _,err := http.Get("https://goodsfdsfgle.com")
  9. if err != nil {
  10. fmt.Fprint(w,"Remote server error")
  11. } else {
  12. fmt.Fprint(w,"Remote server ok!")
  13. }
  14. }
  15. func handleRequests() {
  16. http.HandleFunc("/status", GetStatus)
  17. log.Fatal(http.ListenAndServe(":10000",nil))
  18. }
  19. func main() {
  20. handleRequests()
  21. }

huangapple
  • 本文由 发表于 2021年6月1日 16:55:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/67785747.html
匿名

发表评论

匿名网友

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

确定