Java : HttpHeaders with Examples

HttpHeaders (Java SE 21 & JDK 21) with Examples.
You will find code examples on most HttpHeaders methods.


Summary

A read-only view of a set of HTTP headers.

Class diagram

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri)
        .POST(HttpRequest.BodyPublishers.ofString("xyz"))
        .header("Content-Type", "text/plain; charset=utf-8")
        .header("Content-Language", "en")
        .build();

System.out.println(request); // https://example.com/ POST

final var headers = request.headers();

// {Content-Language=[en], Content-Type=[text/plain; charset=utf-8]}
System.out.println(headers.map());

Methods

List<String> allValues (String name)

Returns an unmodifiable List of all of the header string values of the given named header.

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri)
        .header("aaa", "X")
        .header("bbb", "Y1")
        .header("bbb", "Y2")
        .header("ccc", "Z")
        .build();

final var headers = request.headers();

System.out.println(headers.allValues("aaa")); // [X]
System.out.println(headers.allValues("bbb")); // [Y1, Y2]
System.out.println(headers.allValues("ccc")); // [Z]
System.out.println(headers.allValues("ddd")); // []

final boolean equals (Object obj)

Tests this HTTP headers instance for equality with the given object.

final var headers1 = HttpHeaders.of(
        Map.of(
                "aaa", List.of("X1", "X2"),
                "bbb", List.of("Y")
        ),
        (n, v) -> true);

final var headers2 = HttpHeaders.of(
        Map.of(
                "aaa", List.of("X1", "X2"),
                "bbb", List.of("Y")
        ),
        (n, v) -> true);

final var headers3 = HttpHeaders.of(
        Map.of(
                "aaa", List.of("X1", "X2"),
                "ccc", List.of("Z")
        ),
        (n, v) -> true);

System.out.println(headers1.equals(headers2)); // true
System.out.println(headers1.equals(headers3)); // false

Optional<String> firstValue (String name)

Returns an Optional containing the first header string value of the given named (and possibly multi-valued) header.

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri)
        .header("aaa", "X")
        .header("bbb", "Y1")
        .header("bbb", "Y2")
        .build();

final var headers = request.headers();

System.out.println(headers.firstValue("aaa")); // Optional[X]
System.out.println(headers.firstValue("bbb")); // Optional[Y1]
System.out.println(headers.firstValue("ccc")); // Optional.empty

OptionalLong firstValueAsLong (String name)

Returns an OptionalLong containing the first header string value of the named header field.

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri)
        .header("aaa", "123")
        .header("bbb", "456")
        .header("bbb", "789")
        .build();

final var headers = request.headers();

System.out.println(headers.firstValueAsLong("aaa")); // OptionalLong[123]
System.out.println(headers.firstValueAsLong("bbb")); // OptionalLong[456]
System.out.println(headers.firstValueAsLong("ccc")); // OptionalLong.empty

final int hashCode ()

Computes a hash code for this HTTP headers instance.

final var headers1 = HttpHeaders.of(
        Map.of(
                "aaa", List.of("X1", "X2"),
                "bbb", List.of("Y")
        ),
        (n, v) -> true);

final var headers2 = HttpHeaders.of(
        Map.of(
                "ccc", List.of("Z")
        ),
        (n, v) -> true);

final var headers3 = HttpHeaders.of(
        Map.of(
                "ddd", List.of("123")
        ),
        (n, v) -> true);

System.out.println(headers1.hashCode()); // 107261
System.out.println(headers2.hashCode()); // 98426
System.out.println(headers3.hashCode()); // 81333

Map<String,List<String>> map ()

Returns an unmodifiable multi Map view of this HttpHeaders.

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri)
        .header("aaa", "X")
        .header("bbb", "Y1")
        .header("bbb", "Y2")
        .header("ccc", "Z")
        .build();

final var headers = request.headers();
final var map = headers.map();

System.out.println("-- for --");
for (final var entry : map.entrySet()) {
    System.out.println(entry.getKey() + " : " + entry.getValue());
}

// Result
// ↓
//-- for --
//aaa : [X]
//bbb : [Y1, Y2]
//ccc : [Z]

static HttpHeaders of (Map<String,List<String>> headerMap, BiPredicate<String,String> filter)

Returns an HTTP headers from the given map.

final var headerMap = Map.of(
        "aaa", List.of("X"),
        "bbb", List.of("Y1", "Y2"),
        "ccc", List.of("Z")
);

final var headers1 = HttpHeaders.of(headerMap, (n, v) -> true);
System.out.println(headers1.map()); // {aaa=[X], bbb=[Y1, Y2], ccc=[Z]}

final var headers2 = HttpHeaders.of(headerMap, (n, v) -> false);
System.out.println(headers2.map()); // {}

final var headers3 = HttpHeaders.of(headerMap,
        (name, value) -> !name.equals("aaa"));
System.out.println(headers3.map()); // {bbb=[Y1, Y2], ccc=[Z]}

final var headers4 = HttpHeaders.of(headerMap,
        (name, value) -> value.equals("Z"));
System.out.println(headers4.map()); // {ccc=[Z]}

final var headers5 = HttpHeaders.of(headerMap,
        (name, value) -> value.equals("Y2"));
System.out.println(headers5.map()); // {bbb=[Y2]}

String toString ()

Returns this HTTP headers as a string.

final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri)
        .header("aaa", "X")
        .header("bbb", "Y1")
        .header("bbb", "Y2")
        .header("ccc", "Z")
        .build();

final var headers = request.headers();

// java.net.http.HttpHeaders@31790 { {aaa=[X], bbb=[Y1, Y2], ccc=[Z]} }
System.out.println(headers.toString());

Related posts

To top of page