Consul HTTP requests to get all kv values

13.4k Views Asked by At

I need to get all the consul kv values using http api. Currently I can get the one value with the following command.

curl -k -X GET https://consul.banuka1.us-east-2.test:8543/v1/kv/banuka-test/test-value?token=1995-08-18

it returns only the value specified in it (test-value)

But I want to get all the values in the kv store

How can I do this? Is there a workaround here?

NOTE: I have already done this using consul cli but I want to do this with https api

2

There are 2 best solutions below

0
On

I usually do this to print all the keys and values.

v1/kv/?keys - returns all the keys

I then iterate each key and read the raw response and decode the base64 value.

while read -r key
do    
    value=`curl --silent "http://127.0.0.1:8500/v1/kv/$key" | jq -r '.[].Value' | base64 --decode`
    echo "$key - $value"
done < <(curl --silent "http://127.0.0.1:8500/v1/kv/?keys"| jq -r '.[]')
0
On

You can simplify this and reduce the number of API calls by using the recurse=true query argument.

curl http://127.0.0.1:8500/v1/kv/\?recurse=true | jq -r '.[].Value | @base64d'