diff --git a/server/api/region.go b/server/api/region.go index 42b430974c4..fb1a5a00d34 100644 --- a/server/api/region.go +++ b/server/api/region.go @@ -277,6 +277,17 @@ func (h *regionHandler) GetRegion(w http.ResponseWriter, r *http.Request) { h.rd.JSON(w, http.StatusBadRequest, err.Error()) return } + // decode hex if query has params with hex format + formatStr := r.URL.Query().Get("format") + if formatStr == "hex" { + keyBytes, err := hex.DecodeString(string(key)) + if err != nil { + h.rd.JSON(w, http.StatusBadRequest, err.Error()) + return + } + key = string(keyBytes) + } + regionInfo := rc.GetRegionByKey([]byte(key)) h.rd.JSON(w, http.StatusOK, NewAPIRegionInfo(regionInfo)) } diff --git a/server/api/region_test.go b/server/api/region_test.go index acd305884d4..2550c81e6dd 100644 --- a/server/api/region_test.go +++ b/server/api/region_test.go @@ -147,6 +147,13 @@ func (suite *regionTestSuite) TestRegion() { suite.NoError(tu.ReadGetJSON(re, testDialClient, url, r2)) r2.Adjust() suite.Equal(NewAPIRegionInfo(r), r2) + + url = fmt.Sprintf("%s/region/key/%s?format=hex", suite.urlPrefix, hex.EncodeToString([]byte("a"))) + r2 = &RegionInfo{} + suite.NoError(tu.ReadGetJSON(re, testDialClient, url, r2)) + r2.Adjust() + suite.Equal(NewAPIRegionInfo(r), r2) + } func (suite *regionTestSuite) TestRegionCheck() {