使用 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) } 登录后复制