Go (GoLang) Gorilla/Mux 设置响应代码

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

Go (GoLang) Gorilla/Mux Set Response Code

问题

我在尝试为Gorilla Mux中的路由设置响应代码时遇到了困难。如果我设置一个非常简单的路由,如下所示:

// dummy test route
router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Access-Control-Allow-Headers", "*")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "POST, GET")
    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, `{"message":"OK"}`)
}).Methods("POST")

它似乎工作正常,并返回一个状态码为200,这是可以接受的,但是如果我尝试手动设置响应代码,如下所示:

// dummy test route
router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK) // 添加了这个进行测试
    w.Header().Set("Access-Control-Allow-Headers", "*")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "POST, GET")
    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, `{"message":"OK"}`)
}).Methods("POST")

然后它似乎将响应代码设置为200,但在进行REST请求时仍然显示为错误:

POST *url*/test net::ERR_FAILED 200

我不确定为什么会这样,但这不是设置响应代码的正确方式吗?

英文:

I'm having difficulty while trying to set the Response Code for a Gorilla Mux Route in Go. If I have a very simple route set up as

// dummy test route
router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Access-Control-Allow-Headers", "*")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "POST, GET")
    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, "{\"message\":\"OK\"}")
}).Methods("POST")

It seems to be working fine and returns a Status of 200 which is fine but if I try to set the response code manually like the below.

// dummy test route
router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
    w.WriteHeader(http.StatusOK) // added this to test
    w.Header().Set("Access-Control-Allow-Headers", "*")
    w.Header().Set("Access-Control-Allow-Origin", "*")
    w.Header().Set("Access-Control-Allow-Methods", "POST, GET")
    w.Header().Set("Content-Type", "application/json")
    fmt.Fprintf(w, "{\"message\":\"OK\"}")
}).Methods("POST")

Then it seems to have set the response code to 200 but it still coming through as an error when making a REST request

POST *url*/test net::ERR_FAILED 200

I am not sure why this is but is this not the correct way to set a response code?

答案1

得分: 2

你必须将状态码写在最后,因为WriteHeader函数会将响应写入并刷新。

// 虚拟测试路由
router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Access-Control-Allow-Headers", "*")
        w.Header().Set("Access-Control-Allow-Origin", "*")
        w.Header().Set("Access-Control-Allow-Methods", "POST, GET")
        w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusOK) // 在设置完头部后,设置你的状态码
        fmt.Fprintf(w, `{"message":"OK"}`)
}).Methods("POST")
英文:

You have to write the status code in the end as the WriteHeader function writes to the response and flushes it.

// dummy test route
router.HandleFunc("/test", func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("Access-Control-Allow-Headers", "*")
        w.Header().Set("Access-Control-Allow-Origin", "*")
        w.Header().Set("Access-Control-Allow-Methods", "POST, GET")
        w.Header().Set("Content-Type", "application/json")
        w.WriteHeader(http.StatusOK) // Set your code here, after setting your headers
        fmt.Fprintf(w, "{\"message\":\"OK\"}")
}).Methods("POST")

huangapple
  • 本文由 发表于 2021年12月17日 23:03:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/70395092.html
匿名

发表评论

匿名网友

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

确定