REST API interface to V3

5,981 views
Skip to first unread message

Joseph Swaminathan

unread,
Jan 4, 2017, 12:07:54 AM1/4/17
to etcd-dev

When I write to Etcd using V3 API (golibrary), I am not able to read it through Curl.  I tried changing the "v2" to "v3" in the URL but errors out.  Is REST not supported with V3 interface. 

root@ubuntu-14:~/myapp/src# ETCDCTL_API=3 etcdctl get --from-key square

square/1

{"position":{"x":0,"y":0},"isblack":false,"location":"localhost:80"}

square/2

{"position":{"x":1,"y":0},"isblack":true,"location":"localhost:81"}


root@ubuntu-14:~/myapp/src# curl http://127.0.0.1:2379/v2/keys/sqaure/1

{"errorCode":100,"message":"Key not found","cause":"/sqaure","index":8}


root@ubuntu-14:~/myapp/src# curl http://127.0.0.1:2379/v3/keys/sqaure/1

404 page not found

anthony...@coreos.com

unread,
Jan 4, 2017, 2:34:25 PM1/4/17
to etcd-dev
Hi,

The API is different for v3-- it uses grpc-gateway for JSON clients:
https://github.com/coreos/etcd/blob/master/Documentation/dev-guide/api_grpc_gateway.md

dafe...@gmail.com

unread,
Jan 4, 2017, 3:10:13 PM1/4/17
to etcd-dev

 In etcd v3 using curl , you must give key and value in base64 format.
For instance:
// this command return all keys in a cluster 
// "AA==" is '/0' in base64
curl -L https://127.0.0.1:2379/v3alpha/kv/range -X POST -d '{"key": "AA==", "range_end": "AA=="}'

Using range you can see a range of key or specific key.
// Get a given key, in this case I use foo=bar ------ in base64  foo is "Zm9v"

curl -L https://127.0.0.1:2379/v3alpha/kv/range -X POST -d '{"key": "Zm9v"}'
$ bar

//If both key and range_end are '\0', return all keys
// key is the first key for the range. If range_end is not given, the request only looks up key.
// range_end is the upper bound on the requested range [key, range_end].
// If range_end is '\0', the range is all keys >= key.
// If the range_end is one bit larger than the given key,
// then the range requests get the all keys with the prefix (the given key).


Joseph Swaminathan

unread,
Jan 4, 2017, 4:56:51 PM1/4/17
to etcd-dev
Thanks to both of you for the answers. I am able to see the data now. 

Given that people use REST for both programatic and human interaction, would it be useful to provide an option to accept and return data in plain text ?

thanks
Joseph

anthony...@coreos.com

unread,
Jan 4, 2017, 5:27:13 PM1/4/17
to etcd-dev
> would it be useful to provide an option to accept and return data in plain text ?
It's not planned. The problem with that is v3 uses arbitrary binary strings for keys and values; plaintext strings would violate JSON formatting in general. It's unlikely grpc-gateway will ever support that mode so plaintexted binary fields would need a custom json API and extra code to convert it to grpc. It's a lot of extra work/support for relatively little gain at the moment.

Joseph Swaminathan

unread,
Jan 4, 2017, 6:06:48 PM1/4/17
to etcd-dev
Thanks Anthony for the explanation. It makes sense.  I like V3 API and find it functionally rich and I also liked the simplicity of text base keys and values.. I find V2 API still works on the same ETCD.  Looking at the git, it seems V2 and V3 both are official. Are there plans to keep the V2 API along with V3 API or V2 API will be obsoleted. If I were to start on a new project would you strongly suggest that I use V3. 

thanks
Joseph

anthony...@coreos.com

unread,
Jan 4, 2017, 7:29:13 PM1/4/17
to etcd-dev
v2 is feature frozen and the v2 backend in etcdserver will eventually be deprecated; the v2 backend is much slower than v3. API support for v2 might remain via a translating proxy. Please use v3 if possible.

Joseph Swaminathan

unread,
Jan 4, 2017, 7:48:23 PM1/4/17
to etcd-dev
Thanks. Will use V3 API

Joseph
Reply all
Reply to author
Forward
0 new messages