Java : HttpRequest.Builder with Examples

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


Summary

A builder of HTTP requests.

Class diagram

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

final var builder = HttpRequest.newBuilder(uri);
final var request = builder
        .POST(HttpRequest.BodyPublishers.ofString("XYZ"))
        .header("Content-Type", "text/plain")
        .header("Content-Language", "en")
        .timeout(Duration.ofSeconds(30))
        .version(HttpClient.Version.HTTP_1_1)
        .build();

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

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

System.out.println(request.timeout()); // Optional[PT30S]
System.out.println(request.version()); // Optional[HTTP_1_1]

Methods

HttpRequest build ()

Builds and returns an HttpRequest.

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

final var request = HttpRequest.newBuilder(uri)
        .POST(HttpRequest.BodyPublishers.ofString("XYZ"))
        .build();

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

HttpRequest.Builder copy ()

Returns an exact duplicate copy of this Builder based on current state.

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

final var src = HttpRequest.newBuilder(uri).DELETE();
final var copied = src.copy();

System.out.println(src.build()); // https://example.com/ DELETE
System.out.println(copied.build()); // https://example.com/ DELETE

copied.GET();

System.out.println(src.build()); // https://example.com/ DELETE
System.out.println(copied.build()); // https://example.com/ GET

src.uri(URI.create("http://127.0.0.1:8000/"));

System.out.println(src.build()); // http://127.0.0.1:8000/ DELETE
System.out.println(copied.build()); // https://example.com/ GET

HttpRequest.Builder DELETE ()

Sets the request method of this builder to DELETE.

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

final var request = HttpRequest.newBuilder(uri).DELETE().build();

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

HttpRequest.Builder expectContinue (boolean enable)

Requests the server to acknowledge the request before sending the body.

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

final var request = HttpRequest.newBuilder(uri).expectContinue(true).build();

System.out.println(request); // https://example.com/ GET
System.out.println(request.expectContinue()); // true
final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request.expectContinue()); // false

HttpRequest.Builder GET ()

Sets the request method of this builder to GET.

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

final var request = HttpRequest.newBuilder(uri).GET().build();

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

default HttpRequest.Builder HEAD ()

Sets the request method of this builder to HEAD.

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

final var request = HttpRequest.newBuilder(uri).HEAD().build();

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

HttpRequest.Builder header (String name, String value)

Adds the given name value pair to the set of headers for this request.

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();

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

// {aaa=[X], bbb=[Y1, Y2], ccc=[Z]}
System.out.println(request.headers().map());

HttpRequest.Builder headers (String... headers)

Adds the given name value pairs to the set of headers for this request.

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

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

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

// {aaa=[X], bbb=[Y1, Y2], ccc=[Z]}
System.out.println(request.headers().map());

HttpRequest.Builder method (String method, HttpRequest.BodyPublisher bodyPublisher)

Sets the request method and request body of this builder to the given values.

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

final var request = HttpRequest.newBuilder(uri)
        .method("HEAD", HttpRequest.BodyPublishers.noBody())
        .build();

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

HttpRequest.Builder POST (HttpRequest.BodyPublisher bodyPublisher)

Sets the request method of this builder to POST and sets its request body publisher to the given value.

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

final var request = HttpRequest.newBuilder(uri)
        .POST(HttpRequest.BodyPublishers.ofString("XYZ"))
        .build();

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

HttpRequest.Builder PUT (HttpRequest.BodyPublisher bodyPublisher)

Sets the request method of this builder to PUT and sets its request body publisher to the given value.

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

final var request = HttpRequest.newBuilder(uri)
        .PUT(HttpRequest.BodyPublishers.ofString("XYZ"))
        .build();

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

HttpRequest.Builder setHeader (String name, String value)

Sets the given name value pair to the set of headers for this request.

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

final var builder = HttpRequest.newBuilder(uri);

final var request1 = builder
        .header("aaa", "X1")
        .header("aaa", "X2")
        .header("bbb", "Y")
        .build();

// {aaa=[X1, X2], bbb=[Y]}
System.out.println(request1.headers().map());

final var request2 = builder
        .setHeader("aaa", "X3")
        .build();

// {aaa=[X3], bbb=[Y]}
System.out.println(request2.headers().map());

HttpRequest.Builder timeout (Duration duration)

Sets a timeout for this request.

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

final var request = HttpRequest.newBuilder(uri)
        .timeout(Duration.ofSeconds(30))
        .build();

System.out.println(request); // https://example.com/ GET
System.out.println(request.timeout()); // Optional[PT30S]
final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request.timeout()); // Optional.empty

HttpRequest.Builder uri (URI uri)

Sets this HttpRequest's request URI.

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

final var request = HttpRequest.newBuilder().uri(uri).build();
System.out.println(request); // https://example.com/ GET

HttpRequest.Builder version (HttpClient.Version version)

Sets the preferred HttpClient.Version for this request.

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

final var request = HttpRequest.newBuilder(uri)
        .version(HttpClient.Version.HTTP_1_1)
        .build();

System.out.println(request); // https://example.com/ GET
System.out.println(request.version()); // Optional[HTTP_1_1]
final var uri = URI.create("https://example.com/");

final var request = HttpRequest.newBuilder(uri).build();
System.out.println(request.version()); // Optional.empty

Related posts

To top of page