diff --git a/command/agent/kvs_endpoint.go b/command/agent/kvs_endpoint.go index 2adaae352..dfcce3025 100644 --- a/command/agent/kvs_endpoint.go +++ b/command/agent/kvs_endpoint.go @@ -76,6 +76,16 @@ func (s *HTTPServer) KVSGet(resp http.ResponseWriter, req *http.Request, args *s resp.WriteHeader(404) return nil, nil } + + // Check if we are in raw mode with a normal get, write out + // the raw body + if _, ok := params["raw"]; ok && method == "KVS.Get" { + body := out.Entries[0].Value + resp.Header().Set("Content-Length", strconv.FormatInt(int64(len(body)), 10)) + resp.Write(body) + return nil, nil + } + return out.Entries, nil } diff --git a/command/agent/kvs_endpoint_test.go b/command/agent/kvs_endpoint_test.go index 867b2a01f..0883cfaaa 100644 --- a/command/agent/kvs_endpoint_test.go +++ b/command/agent/kvs_endpoint_test.go @@ -409,3 +409,37 @@ func TestKVSEndpoint_AcquireRelease(t *testing.T) { } }) } + +func TestKVSEndpoint_GET_Raw(t *testing.T) { + httpTest(t, func(srv *HTTPServer) { + buf := bytes.NewBuffer([]byte("test")) + req, err := http.NewRequest("PUT", "/v1/kv/test", buf) + if err != nil { + t.Fatalf("err: %v", err) + } + resp := httptest.NewRecorder() + obj, err := srv.KVSEndpoint(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + if res := obj.(bool); !res { + t.Fatalf("should work") + } + + req, err = http.NewRequest("GET", "/v1/kv/test?raw", nil) + if err != nil { + t.Fatalf("err: %v", err) + } + resp = httptest.NewRecorder() + obj, err = srv.KVSEndpoint(resp, req) + if err != nil { + t.Fatalf("err: %v", err) + } + assertIndex(t, resp) + + // Check the body + if !bytes.Equal(resp.Body.Bytes(), []byte("test")) { + t.Fatalf("bad: %s", resp.Body.Bytes()) + } + }) +} diff --git a/website/source/docs/agent/http.html.markdown b/website/source/docs/agent/http.html.markdown index c220d60bd..f7255e3de 100644 --- a/website/source/docs/agent/http.html.markdown +++ b/website/source/docs/agent/http.html.markdown @@ -145,6 +145,10 @@ For example, listing "/web/" with a "/" seperator may return: Using the key listing method may be suitable when you do not need the values or flags, or want to implement a key-space explorer. +If the "?raw" query parameter is used with a non-recursive GET, +then the response is just the raw value of the key, without any +encoding. + If no entries are found, a 404 code is returned. This endpoint supports blocking queries and all consistency modes.