From 2a1a14ff37a26ca6a76763ecc33727c23d9284e9 Mon Sep 17 00:00:00 2001 From: Jia-ben00 <2403877005@qq.com> Date: Wed, 16 Sep 2026 21:24:33 +0800 Subject: [PATCH] feat(router): add QUERY method shortcut for RFC 10008 (#4830) * feat(router): add QUERY method shortcut for RFC 10008 Add a QUERY() router method as a shortcut for Handle("QUERY", path, handlers), following the same pattern as GET, POST, HEAD and other HTTP method shortcuts. The QUERY method is defined by RFC 10008 and allows sending a query in the request body, similar to GET but with a body. Go's net/http does not yet expose an http.MethodQuery constant, so the literal string "QUERY" is used. Closes #4721 * style(router): format QUERY method test - Remove an extra blank line to satisfy gofmt. --------- Co-authored-by: Jia-ben00 <316397950+Jia-ben00@users.noreply.github.com> Co-authored-by: Bo-Yi Wu --- gin_test.go | 19 +++++++++++++++++++ routergroup.go | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/gin_test.go b/gin_test.go index a9cf1755..b488c446 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1156,3 +1156,22 @@ func TestUpdateRouteTreesCalledOnce(t *testing.T) { assert.Equal(t, "ok", w.Body.String()) } } + +func TestRouterQueryMethod(t *testing.T) { + router := New() + router.QUERY("/test", func(c *Context) { + c.String(http.StatusOK, "query ok") + }) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("QUERY", "/test", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "query ok", w.Body.String()) + + // Other methods should not match + w2 := httptest.NewRecorder() + req2, _ := http.NewRequest(http.MethodGet, "/test", nil) + router.ServeHTTP(w2, req2) + assert.Equal(t, http.StatusNotFound, w2.Code) +} diff --git a/routergroup.go b/routergroup.go index c01b917e..1ae0b7b4 100644 --- a/routergroup.go +++ b/routergroup.go @@ -142,6 +142,13 @@ func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRo return group.handle(http.MethodHead, relativePath, handlers) } +// QUERY is a shortcut for router.Handle("QUERY", path, handlers). +// The QUERY method is defined by RFC 10008 and is used to send a query +// in the request body, similar to GET but with a body. +func (group *RouterGroup) QUERY(relativePath string, handlers ...HandlerFunc) IRoutes { + return group.handle("QUERY", relativePath, handlers) +} + // Any registers a route that matches all the HTTP methods. // GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE. func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {