Java 21 HttpClient: Intermittent java.io.IOException: HTTP/2 stream was reset when POSTing JSON how to make it reliable?
12:51 04 Mar 2026

I’m building a small Java service (Java 21) that calls a third-party REST API. I’m using and sending JSON with POST. The request works most of the time, but intermittently fails with an IOException / HTTP/2 reset.
Code

import java.net.URI;
import java.net.http.*;
import java.time.Duration;

public class ApiClient {
    private static final HttpClient CLIENT = HttpClient.newBuilder()
            .connectTimeout(Duration.ofSeconds(5))
            .version(HttpClient.Version.HTTP_2)
            .build();

    public static void main(String[] args) throws Exception {
        String json = """
            {"userId":123,"action":"ping","timestamp":"2026-03-04T10:00:00Z"}
        """;

        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create("https://api.example.com/v1/events"))
                .timeout(Duration.ofSeconds(10))
                .header("Content-Type", "application/json")
                .header("Authorization", "Bearer REDACTED")
                .POST(HttpRequest.BodyPublishers.ofString(json))
                .build();

        HttpResponse response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
        System.out.println("Status: " + response.statusCode());
        System.out.println(response.body());
    }
}
java