Skip to content

Java API Client Example

This example demonstrates how to send an authenticated HTTP request to the Core API using Java’s built-in HttpClient, introduced in Java 11.

The example performs a GET request using Bearer token authentication.


Overview

The Java HttpClient API is part of the standard library and provides a modern interface for sending HTTP requests and handling responses.

It supports:

  • Synchronous and asynchronous requests
  • Custom headers and authentication
  • HTTP/1.1 and HTTP/2

Example Request

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class ApiQuery {

    public static void main(String[] args) throws Exception {

        String baseUrl = "https://website.core-software.co.uk";
        String token = "YOUR_TOKEN_HERE";

        HttpClient client = HttpClient.newHttpClient();

        HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create(baseUrl + "/api/v1"))
            .header("Authorization", "Bearer " + token)
            .header("Content-Type", "application/json")
            .GET()
            .build();

        HttpResponse<String> response = client.send(
            request,
            HttpResponse.BodyHandlers.ofString()
        );

        System.out.println("Status: " + response.statusCode());
        System.out.println("Body: " + response.body());
    }
}

Notes

  • Replace YOUR_TOKEN_HERE with a valid API token before running the example
  • The example sends a simple GET request to the API root endpoint
  • Authentication is provided using the Authorization: Bearer header
  • The response status code and body are printed to the console
  • Error handling is intentionally minimal for demonstration purposes