diff --git a/server/api/region.go b/server/api/region.go index 69dc0802923..cf675d7ecb0 100644 --- a/server/api/region.go +++ b/server/api/region.go @@ -16,6 +16,7 @@ package api import ( "container/heap" "net/http" + "net/url" "sort" "strconv" @@ -145,6 +146,11 @@ func (h *regionHandler) GetRegionByKey(w http.ResponseWriter, r *http.Request) { rc := getCluster(r.Context()) vars := mux.Vars(r) key := vars["key"] + key, err := url.QueryUnescape(key) + if err != nil { + h.rd.JSON(w, http.StatusBadRequest, err.Error()) + return + } regionInfo := rc.GetRegionInfoByKey([]byte(key)) h.rd.JSON(w, http.StatusOK, NewRegionInfo(regionInfo)) } diff --git a/server/api/router.go b/server/api/router.go index 081a1f9a736..b09e7bd629e 100644 --- a/server/api/router.go +++ b/server/api/router.go @@ -124,7 +124,7 @@ func createRouter(ctx context.Context, prefix string, svr *server.Server) *mux.R regionHandler := newRegionHandler(svr, rd) clusterRouter.HandleFunc("/region/id/{id}", regionHandler.GetRegionByID).Methods("GET") - clusterRouter.HandleFunc("/region/key/{key}", regionHandler.GetRegionByKey).Methods("GET") + clusterRouter.UseEncodedPath().HandleFunc("/region/key/{key}", regionHandler.GetRegionByKey).Methods("GET") srd := createStreamingRender() regionsAllHandler := newRegionsHandler(svr, srd) diff --git a/tests/pdctl/region/region_test.go b/tests/pdctl/region/region_test.go index 972890653c8..8d521c44107 100644 --- a/tests/pdctl/region/region_test.go +++ b/tests/pdctl/region/region_test.go @@ -236,6 +236,14 @@ func (s *regionTestSuite) TestRegion(c *C) { c.Assert(json.Unmarshal(output, ®ionInfo), IsNil) c.Assert(®ionInfo, DeepEquals, api.NewRegionInfo(r2)) + // issue #2351 + args = []string{"-u", pdAddr, "region", "key", "--format=hex", "622f62"} + _, output, err = pdctl.ExecuteCommandC(cmd, args...) + c.Assert(err, IsNil) + regionInfo = api.RegionInfo{} + c.Assert(json.Unmarshal(output, ®ionInfo), IsNil) + c.Assert(®ionInfo, DeepEquals, api.NewRegionInfo(r2)) + // region startkey --format=raw command args = []string{"-u", pdAddr, "region", "startkey", "--format=raw", "b", "2"} _, output, err = pdctl.ExecuteCommandC(cmd, args...)