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

2024年 2月 11日 100.0k 0

使用 post 添加到 get 相同的端点和不同的查询最终会出现不一致的错误消息

php小编香蕉在此为大家解答一个常见问题:使用 POST 添加到 GET 相同的端点和不同的查询,最终可能会出现不一致的错误消息。在Web开发中,GET和POST是常用的HTTP请求方法,用于向服务器传递数据。GET方法将数据附加在URL中,而POST方法将数据封装在请求正文中。当我们将POST请求添加到GET请求的相同端点时,如果查询参数不同,可能会导致错误消息不一致的情况发生。这是因为服务器会根据请求方法和查询参数来处理请求,不同的查询参数可能会导致服务器返回不同的结果。因此,在使用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)

}

登录后复制

以上就是使用 POST 添加到 GET 相同的端点和不同的查询最终会出现不一致的错误消息的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论