1

Am traying to hit a post endpoint but It is giving error 302, When I tried another get Url on the same server it gives me 200. Then I redirected the post request using LaxRedirectStrategy() The post request is redirecting to the get request(same endpoint only method name is GET and POST) it is not getting response from the post method. Can anyone tell me how to redirect post request to post request using apahce httpClient 4.5

HttpClient client= HttpClientBuilder.create()
 .setRedirectStrategy(new LaxRedirectStrategy()).build();
HttpPost post = new HttpPost("url");
post.addHeader("content-type", " application/json");
HttpResponse response = client.execute(post);
Sameesh
  • 315
  • 8
  • 18
  • whats is the error you are getting from the POST request? 302? – Adi Ohana May 26 '19 at 11:48
  • Yes, 302 redirect – Sameesh May 26 '19 at 13:55
  • just to be on the safe side, you can try handle the redirect in the "old" way [check if status is 3XX and look for the redirect host in location header] to make sure that the issue is not in your server. https://stackoverflow.com/questions/8014997/httppost-redirect-location-or-body-of-response-needed – Adi Ohana May 26 '19 at 13:59

2 Answers2

2

I had the same issue I solved it by using using LaxRedirectStrategy with overridden getRedirect method.

Apparently the default behaviour for POST requests is to make the redirected call as a GET request when the initial redirect response is different than 307 or 308.

See: DefaultRedirectStrategy which LaxRedirectStrategy inherits from.

In my case the redirect response code was a 302.

So if you want something different, you can just override the getRedirect method and provide your own implementation.

Something like:

new LaxRedirectStrategy() {
        @Override
        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException {
            final URI uri = getLocationURI(request, response, context);
            final String method = request.getRequestLine().getMethod();
            if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
                return new HttpHead(uri);
            } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
                return new HttpGet(uri);
            } else {
                final int status = response.getStatusLine().getStatusCode();
                if (status == HttpStatus.SC_TEMPORARY_REDIRECT || status == HttpStatus.SC_MOVED_TEMPORARILY) { //HttpStatus.SC_MOVED_TEMPORARILY == 302
                    return RequestBuilder.copy(request).setUri(uri).build();
                } else {
                    return new HttpGet(uri);
                }
            }
        }
    }
1
    HttpClient httpClient =
        HttpClients.custom().setRedirectStrategy(new LaxRedirectStrategy() {

            /*
             * (non-Javadoc)
             * 
             * @see org.apache.http.impl.client.DefaultRedirectStrategy#
             * getRedirect(org.apache.http.HttpRequest,
             * org.apache.http.HttpResponse,
             * org.apache.http.protocol.HttpContext)
             */
            @Override
            public HttpUriRequest getRedirect(
                HttpRequest request, HttpResponse response,
                HttpContext context) throws ProtocolException
        {

                final URI uri = getLocationURI(request, response, context);
                final String method = request.getRequestLine().getMethod();
                if (method.equalsIgnoreCase(HttpPost.METHOD_NAME)) {

                    HttpPost post = new HttpPost(uri);
                    post.setEntity(entity);
                    return post;
                } else if (method.equalsIgnoreCase(HttpHead.METHOD_NAME)) {
                    return new HttpHead(uri);
                } else if (method.equalsIgnoreCase(HttpGet.METHOD_NAME)) {
                    return new HttpGet(uri);
                } else {
                    final int status =
                        response.getStatusLine().getStatusCode();
                    return status == HttpStatus.SC_TEMPORARY_REDIRECT
                        ? RequestBuilder.copy(request).setUri(uri).build()
                        : new HttpGet(uri);
                }
            }

        })
srinu rao
  • 11
  • 1