How to get the modify index of a key/value after setting it?

When I set a key in consul, e.g.:

curl -w "\n" -v --request PUT --data value http://127.0.0.1:8500/v1/kv/my-key?cas=1234

How can I get the index for the value I just set? I was surprised to see the response doesn’t contain the header X-Consul-Index (why not?).

If I issue another request, e.g.:

curl -w "\n" -v http://127.0.0.1:8500/v1/kv/my-key

I will get the index for the current value but there are two problems with that:

  1. It’s a race condition. The key could have been changed in the meantime and I’ll get a different index.
  2. It’s wasteful, the response should have that info.

What’s the correct way of setting a key/value and knowing the index?

Hi @balshetzer,

The workflow you’re using is currently the best way to update a key and then obtain the new ModifyIndex.

The shortcomings you’ve identified have already been reported in the following GitHub issue.

I recommend upvoting that issue so that we can better track community interest in this enhancement.

Thanks for the response. I’ve upvoted the issue. I hope you’re able to get to it to allow optimistic updates.