Interfacing With Services

The Knomi Voice Matcher service uses rest API and so can be accessed by any http client such as curl, jmeter, postman or any programming languages which can package and send a POST http request.

Example code that packages the JSON request, sends the request to the server, and parses the result for the following languages/tools. These examples used a json file (request_body.json present in the installation directory) in the following format.

{
    "encounter" : {
        "voiceSamples": [
            {
                "data": "<base64 encoded voice sample>",
                "voice_sample_type": "STATIC_PHRASE"
            }],
            "id": "test_id_001"
    }
}

JavaScript

sample_voice_match.js
   var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
   var xhr  = new XMLHttpRequest()
   xhr.open('POST', url)
   xhr.onload = function () {
       console.log('status' + ' : ' + xhr.status);
       console.log('response content' + ' : ' + xhr.responseText);
   }

   xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
   xhr.send(JSON.stringify(<copy content from request_body.json or read from the file inside the script>));

Example command:

node sample_voice_match.js

Example output:

status : 500
response content : {"error":{"code":1006,"description":"The ID already exists in the cache."}}

Java

sample_voice_match.java
   class sample_voice_match {
       public static void main(String[] args) throws Exception {
           String url = "http://localhost:8071/nexavoice/add/cache1";
           URL obj = new URL(url);
           HttpURLConnection con = (HttpURLConnection) obj.openConnection();
           con.setRequestMethod("POST");
           con.setRequestProperty("Content-Type", "application/json; utf-8");
           con.setRequestProperty("Accept", "application/json");
           con.setDoOutput(true);

           Path request_body = Path.of("request_body.json");
           String jsonInputString = Files.readString(request_body);

           try(OutputStream os = con.getOutputStream()) {
               byte[] input = jsonInputString.getBytes("utf-8");
               os.write(input, 0, input.length);
           }

           int responseCode = con.getResponseCode();
           System.out.println("GET Response Code :: " + responseCode);
           if (responseCode == HttpURLConnection.HTTP_OK) { // success
               BufferedReader in = new BufferedReader(new InputStreamReader(
                       con.getInputStream()));
               String inputLine;
               StringBuffer response = new StringBuffer();

               while ((inputLine = in.readLine()) != null) {
                   response.append(inputLine);
               }
               in.close();

               // print result
               System.out.println(response.toString());
           } else {
               System.out.println("POST request did not work");
           }
           System.out.println("done .... "); // Display the string.

       }
   }

sample output

GET Response Code :: 200
{"id":"test_id_001"}
done ....

Curl Example

sample usage

Example command:

curl -H "Content-Type: application/json" --data @package.json http://localhost:8071/nexavoice/add/cache1

sample response json

{"id":"test_id_001"}