使用POST和不同的查询向GET添加相同的端点,最终会得到不一致的错误消息



当用不同的方法添加相同的路由时,每个方法查询的get调用的响应是不同的,但是由于另一个方法是POST,它应该不受影响。

文章:操场上:https://go.dev/play/p/xzoAkpEhGgy


// You can edit this code!
// Click here and start typing.
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()

r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
fmt.Fprintf(w, "You made a POST request")
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}).Methods("POST")

r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
fmt.Fprintf(w, "You made a GET request")
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}).
Queries("from", "{from:[0-9]+}",
"to", "{to:[0-9]+}").Methods("GET")


srv := &http.Server{
Handler: r,
Addr:    "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout:  15 * time.Second,
}
go srv.ListenAndServe()


req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
out2 := httptest.NewRecorder()

r.ServeHTTP(out2, req2)

fmt.Println(out2.Code)
fmt.Println(out2)
}

预期404,实际405

同时删除POST操场上:https://go.dev/play/p/EXiF00_WrFW

// You can edit this code!
// Click here and start typing.
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
fmt.Fprintf(w, "You made a GET request")
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}).
Queries("from", "{from:[0-9]+}",
"to", "{to:[0-9]+}").Methods("GET")

srv := &http.Server{
Handler: r,
Addr:    "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout:  15 * time.Second,
}
go srv.ListenAndServe()

req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
out2 := httptest.NewRecorder()
r.ServeHTTP(out2, req2)
fmt.Println(out2.Code)
}

404

对于GET请求,路由和结果应该一致。404 - s

我很好奇以前是否有人见过这个问题?

路由器将尝试根据路径和查询参数查找匹配。不匹配GET路由,因为查询字符串参数不匹配。

然而,路径匹配POST路由,因为该路由不关心这些查询参数。然后检查请求方法,如果它与路由不匹配,则返回405 Method Not Allowed(该路由有一个处理程序,但用于不同的方法)。

您可以通过为同一路径添加捕获所有get处理程序来获得所需的行为:

// You can edit this code!
// Click here and start typing.
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"time"
"github.com/gorilla/mux"
)
func main() {
r := mux.NewRouter()
r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
fmt.Fprintf(w, "You made a POST request")
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}).Methods("POST")
r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
// an example API handler
fmt.Fprintf(w, "You made a GET request")
json.NewEncoder(w).Encode(map[string]bool{"ok": true})
}).
Queries("from", "{from:[0-9]+}",
"to", "{to:[0-9]+}").
Methods("GET")
r.HandleFunc("/api/v2", func(w http.ResponseWriter, r *http.Request) {
http.Error(w, "", http.StatusNotFound)
}).Methods("GET")
srv := &http.Server{
Handler: r,
Addr:    "127.0.0.1:8000",
// Good practice: enforce timeouts for servers you create!
WriteTimeout: 15 * time.Second,
ReadTimeout:  15 * time.Second,
}
go srv.ListenAndServe()
req2 := httptest.NewRequest("GET", "/api/v2?from=3&to=-5", nil)
out2 := httptest.NewRecorder()
r.ServeHTTP(out2, req2)
fmt.Println(out2.Code)
}

最新更新