Calling the Turing Robot API with Java HttpClient: A Complete Code Walkthrough

This guide shows how to replace outdated GET calls with a POST request to the Turing Robot API using Java's HttpClient, explains recent changes to the service, and provides a full, ready‑to‑run code example for retrieving chatbot replies.

FunTester
FunTester
FunTester
Calling the Turing Robot API with Java HttpClient: A Complete Code Walkthrough

While experimenting with the Turing chatbot, I discovered that the previous GET‑based examples no longer work and that free, non‑authenticated users have lost their request quota, so a proper POST request with a valid API key is required.

The service now mandates an authenticated key and a JSON payload sent via POST.

Using Apache HttpClient's EntityUtils to parse the response works well, although the sample code is a bit dated.

Below is a complete Java method that builds the JSON payload, sends it to http://www.tuling123.com/openapi/api, reads the response stream, and extracts the chatbot's reply.

public static String getReplyFromRobot(String text) throws JSONException, ClientProtocolException, IOException {
    String url = "http://www.tuling123.com/openapi/api"; // API endpoint
    CloseableHttpClient httpClient = HttpClients.createDefault(); // create client
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("key", "915b34e69c0371"); // your API key
    jsonObject.put("info", text); // user message
    // jsonObject.put("loc", "Beijing Zhongguancun"); // optional location
    jsonObject.put("userid", "915b34e41cb351c0371"); // user ID
    String arguments = changeJsonToArguments(jsonObject); // convert JSON to query string
    HttpPost httpPost = new HttpPost(url + arguments); // POST request
    HttpResponse response = httpClient.execute(httpPost); // get response
    InputStream inputStream = response.getEntity().getContent();
    InputStreamReader reader = new InputStreamReader(inputStream, "utf-8");
    StringBuffer buffer = new StringBuffer();
    char[] buff = new char[512];
    int length = 0;
    while ((length = reader.read(buff)) != -1) {
        String x = new String(buff, 0, length);
        System.out.println(x);
        buffer.append(x);
    }
    JSONObject dsa = new JSONObject(buffer.toString().trim());
    String message = dsa.getString("text");
    return message;
}

Replace the placeholder key with your own Turing API key, adjust the userid if needed, and the method will return the chatbot's textual reply.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

HTTPAPITutorialChatbotHttpClientTuringRobot
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.