This is first tutorial of consul rest api uses and example.This consul rest api tutorial help to get key value of store that can be a JSON file.The KV is useful for storing service configuration or other metadata.The Values in the KV store cannot be larger than 512kb.

Each data-center has its own KV store, and there is no built-in replication between datacenters, if you need replication of KV store between Datacenter,then you can access replication using consul-replicate daemon process.consul-replicate daemon Help to replicate values from one Consul datacenter to another.

Consul is developed by Hasicorp, It is a tool for discovering and configuring services in your infrastructure. It provides several key features:

  • Service Discovery
  • Health Checking
  • KV Store
  • Multi Datacenter

Consul is providing RESTful HTTP API. The API can perform basic CRUD operations on nodes, services, checks and configuration etc.The All API routes are prefixed with v1/ like 'http://consul_hostname/v1/' .The Rest Client Request can be used with Consul token for authentication process, if the authentication is enabled.

Get KV Store Information Using Consul RESTful Api

We will create PHP client to get consul API to get data from consul server.We are using Guzzle PHP rest client to access consul api data.We will use '/kv' end point to get valye of key.You can also passed some option like fetch all keys and values of particular key using recursive flag '/kv/keyname/?recurse&raw' .This tutorial help to get value of particular key.
At the the recursive feathing data you must be encode base64 of value to get original value of key.

require_once 'vendor/autoload.php';
use GuzzleHttp\Client;
$token = base64_encode('username': 'password');
$client = new Client([
// Base URI is used with relative requests
'base_uri' => 'restapi url', //https://consul_hostname/v1/
// You can set any number of default request options.
'timeout'  => 2.0,
'headers' => ['Authorization' => "Basic " . $token, 'Content-Type' => 'application/json', "Accept" => "application/json"],
//ssl false
'verify' => false
$response = $client->get('kv/kename');
echo "
echo $response->getBody()->getContents();

Please change token string as per your REST API credentials.The parameters are:

  • $token : optional, if the authentication enabled.
  • base_uri : The Rancher API host api url path.
  • headers : This will contains request header parameters.
  • timeout : Request timeout in sec.
  • verify : SSL verification false.
  • Genislab Technologies

    NexGeneration complete end-2-end software testing & modern development operations tooling & solutions

    Do you want to discuss your testing requirements with us? please don't hesitate to hit the contact us button below, and we will get back to you at our earliest..