subro
subro

Reputation: 114

Creating a Custom Apache HTTP Client Redirect policy

Need little help, i might sound little confusing..

I am using Apache HTTP Client 4.5.1, I have a post url which needed some header to be used to call, once we call post url it return 303 and a get url, but As per HTTP Client redirect policy its automatically does redirect GET CALL but it send header also in the redirected GET CALL.

How do i override redirect policy of HTTP Client so that 303 redirect does not use header when calling redirect GET URL.

Any idea how to achieve this. I have already checked few option with redirect policy.

Upvotes: 1

Views: 1060

Answers (1)

ok2c
ok2c

Reputation: 27538

The culprit is this line of code in the RedirectExec request execution interceptor.

One should be able to prevent it from copying the original request headers by adding some harmless dummy request header with a custom redirect strategy

CloseableHttpClient client = HttpClients.custom()
        .setRedirectStrategy(new DefaultRedirectStrategy() {

            @Override
            public HttpUriRequest getRedirect(
                    HttpRequest request,
                    HttpResponse response,
                    HttpContext context) throws ProtocolException {
                HttpUriRequest redirectRequest = super.getRedirect(request, response, context);
                redirectRequest.addHeader("x-custom", "stuff");
                return redirectRequest;
            }
        })
        .build();

Upvotes: 2

Related Questions