Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Overview

This quick tutorial will show how to configure the Apache HttpClient to automatically follow redirects for POST requests.

If you want to dig deeper and learn other cool things you can do with the HttpClient – head on over to the main HttpClient tutorial.

2. Redirecting on HTTP POST

2.1. For HttpClient 5.x

By default, both GET and POST requests resulting in a redirect are automatically followed. This functionality is different from the previous version(4.5.x) which we will showcase in the next section.

@Test
void givenRedirectingPOST_whenUsingDefaultRedirectStrategy_thenRedirected() throws IOException {

    final HttpPost request = new HttpPost("http://t.co/I5YYd9tddw");

    try (CloseableHttpClient httpClient = HttpClientBuilder.create()
        .setRedirectStrategy(new DefaultRedirectStrategy())
        .build()) {
        httpClient.execute(request, response -> {
            assertThat(response.getCode(), equalTo(200));
            return response;
        });
    }
}

Notice that, with the DefaultRedirectStrategy and the redirect is followed over POST – leading to a 200 OK status code.

Even if we do not use the new DefaultRedirectStrategy() the redirect is followed:

@Test
void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() throws IOException {

    final HttpPost request = new HttpPost("http://t.co/I5YYd9tddw");

    try (CloseableHttpClient httpClient = HttpClientBuilder.create()
        .build()) {
        httpClient.execute(request, response -> {
            assertThat(response.getCode(), equalTo(200));
            return response;
        });
    }
}

2.2. For HttpClient 4.5

In HttpClient 4.5 , by default, only GET requests resulting in a redirect are automatically followed. If a POST request is answered with either HTTP 301 Moved Permanently or with 302 Found – the redirect is not automatically followed.

This is specified by the HTTP RFC 2616:

If the 301 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

There are of course use cases where we need to change that behaviour and relax the strict HTTP specification.

First, let’s check the default behaviour:

@Test
public void givenPostRequest_whenConsumingUrlWhichRedirects_thenNotRedirected() 
  throws ClientProtocolException, IOException {
    HttpClient instance = HttpClientBuilder.create().build();
    HttpResponse response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw"));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(301));
}

As you can see, the redirect is not followed by default, and we get back the 301 Status Code.

Let’s now see how we can follow redirects by setting a Redirect Strategy:

@Test
public void givenRedirectingPOST_whenConsumingUrlWhichRedirectsWithPOST_thenRedirected() 
  throws ClientProtocolException, IOException {
    HttpClient instance = 
      HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build();
    HttpResponse response = instance.execute(new HttpPost("http://t.co/I5YYd9tddw"));
    assertThat(response.getStatusLine().getStatusCode(), equalTo(200));
}

With the LaxRedirectStrategy, the HTTP Restrictions are relaxed and the redirect is followed over POST as well – leading to a 200 OK status code.

3. Conclusion

This quick guide illustrated how to configure any version of the Apache HttpClient to follow redirects for HTTP POST requests as well – relaxing the strict HTTP standard.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW