๐Ÿ“ฅList, Fetch, and Download Documents

This snippet mirrors the document retrieval recipe using Java.

// Requires Java 17+ and the java.net.http module.
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;

public class RetrieveAndDownloadDocumentSample {
    private static String extensionFromContentType(String contentType) {
        if (contentType == null) return ".txt";
        if (contentType.contains("pdf")) return ".pdf";
        if (contentType.contains("html")) return ".html";
        return ".txt";
    }

    public static void main(String[] args) throws Exception {
        String token = System.getenv().getOrDefault("AUTOCONTENT_TOKEN", "YOUR_API_TOKEN");
        String documentId = System.getenv().getOrDefault("DOCUMENT_ID", "YOUR_REQUEST_ID");
        String baseUrl = "https://api.autocontentapi.com";

        HttpClient client = HttpClient.newHttpClient();

        HttpRequest listRequest = HttpRequest.newBuilder()
                .uri(URI.create(baseUrl + "/documents/get?page=1&pageSize=10"))
                .header("Authorization", "Bearer " + token)
                .header("Accept", "application/json")
                .GET()
                .build();
        HttpResponse<String> listResponse = client.send(listRequest, HttpResponse.BodyHandlers.ofString());
        System.out.println("Documents page:");
        System.out.println(listResponse.body());

        HttpRequest oneRequest = HttpRequest.newBuilder()
                .uri(URI.create(baseUrl + "/documents/" + documentId))
                .header("Authorization", "Bearer " + token)
                .header("Accept", "application/json")
                .GET()
                .build();
        HttpResponse<String> oneResponse = client.send(oneRequest, HttpResponse.BodyHandlers.ofString());
        System.out.println("Single document:");
        System.out.println(oneResponse.body());

        HttpRequest downloadRequest = HttpRequest.newBuilder()
                .uri(URI.create(baseUrl + "/documents/" + documentId + "/download"))
                .header("Authorization", "Bearer " + token)
                .GET()
                .build();
        HttpResponse<byte[]> downloadResponse = client.send(downloadRequest, HttpResponse.BodyHandlers.ofByteArray());

        if (downloadResponse.statusCode() >= 400) {
            System.err.println("Download failed: " + downloadResponse.statusCode());
            System.exit(1);
        }

        String contentType = downloadResponse.headers().firstValue("content-type").orElse("text/plain");
        String extension = extensionFromContentType(contentType);
        Path outputPath = Path.of("briefing-document-" + documentId + extension);
        Files.write(outputPath, downloadResponse.body());
        System.out.println("Saved " + outputPath);
    }
}

Set YOUR_API_TOKEN (or AUTOCONTENT_TOKEN) and DOCUMENT_ID before running.

See also

Last updated