consul batch KV pairs HTTP post using HTTP api (Import a huge json through HTTP API)

835 Views Asked by At

I have application configuration in a json with upto 80 key/value pairs per app, which are to be stored in Consul KV Store. Generally they are java keyvalue properties, I have a simple shell script, which does encode value and convert it into import compatible for consul import command. I am trying to automate the consul KV import for the new apps that on-board (apps that start using consul for KV Store). So far we are manually running the consul kv import @app_config.json however, I would like to call HTTP api rather importing through consul KV import. Any suggestions please?

example for app config in json format.

[
        {
                "key": "asia",
                "flags": 0,
                "value": "NDQzNg=="
        },
        {
                "key": "asia/india",
                "flags": 0,
                "value": "MTMyNA=="
        },
        {
                "key": "europe",
                "flags": 0,
                "value": "NzQzLjE="
        },
        {
                "key": "europe/france",
                "flags": 0,
                "value": "NjYuOQ=="
        },
        {
                "key": "europe/germany",
                "flags": 0,
                "value": "ODIuNjc="
        }
]
1

There are 1 best solutions below

0
On

Basically you need to send PUT /v1/txn request with payload containing the array of transaction operations, like

const url = 'http://127.0.0.1:8500/v1/txn';

const bulk = [
  {
    "KV": {
      "Verb": "set",
      "Key": "bulk/hello",
      "Value": "aGVsbG8="
    }
  },
  {
    "KV": {
      "Verb": "set",
      "Key": "bulk/world",
      "Value": "d29ybGQ="
    }
  }
];

const options = {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json;charset=UTF-8'
  },
  body: JSON.stringify(bulk)
};

await fetch(url, options);