How to use rest-api with Java HttpClient?

Hello,

I want to write a script in Java for checking content in our confluence cloud automatically. First I tried it out with postman and that worked very well.

I then tried to write this in Java to add analyses to responses but my GET request is not working. I checked atlassian and java documentation but I don’t understand why it’s not working. Can someone give me a hint?

HttpClient httpClient = HttpClient.newBuilder()
            .version(HttpClient.Version.HTTP_2)
            .build();

HttpRequest request = HttpRequest.newBuilder()
    .GET()
    .uri(URI.create("https://mycompany.atlassian.net/wiki/rest/api/content/"))
    .setHeader("Authorization", "Basic username:api-token)
    .setHeader("Content-Type", "application/json")
    .build();

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

Thanks Marco

Okay, I changed to .version(HttpClient.Version.HTTP_1_1) and now it’s working.

2 Likes

Basic authorization seems doesn’t work any more