4

I know this is very weird, but in my case I want to repeat a request with additional header (actually credentials) whenever the first request got a 403 status...

I was configuring HttpClient as below, it works for all GET requests, but failed for POST requests with the below exception.

.setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {

                    @Override
                    public boolean retryRequest(HttpResponse response, int executionCount, HttpContext context) {
                        boolean shouldRetry = response.getStatusLine().getStatusCode() == HttpStatus.SC_UNAUTHORIZED
                                && executionCount < 2;
                        if (shouldRetry) {
                            _logger.warn("Got 401 status, retry request with vmware-api-session-id header");
                            context.setAttribute(REQ_ATTR_USING_SESSIONID_HEADER, Boolean.TRUE);
                        } else {
                            context.removeAttribute(REQ_ATTR_USING_SESSIONID_HEADER);
                        }
                        return shouldRetry;
                    }

                    @Override
                    public long getRetryInterval() {
                        return 0;
                    }
                })
                .addInterceptorFirst(new HttpRequestInterceptor() {

                    @Override
                    public void process(HttpRequest request, HttpContext context)
                            throws HttpException, IOException {
                        Boolean withSessionIdHeader = (Boolean) context
                                .getAttribute(REQ_ATTR_USING_SESSIONID_HEADER);
                        if (withSessionIdHeader == null || !withSessionIdHeader) {
                            if (_logger.isDebugEnabled()) {
                                _logger.debug("Removing vmware-api-session-id header");
                            }
                            request.removeHeaders(ServiceProxyController.SESSION_ID_HEADER_NAME);
                        }
                    }
                })

And the exception is:

Caused by: org.apache.http.client.NonRepeatableRequestException: Cannot retry request with a non-repeatable request entity
    at org.apache.http.impl.execchain.RetryExec.execute(RetryExec.java:108)
    at org.apache.http.impl.execchain.ServiceUnavailableRetryExec.execute(ServiceUnavailableRetryExec.java:85)
    at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:185)
    ... 51 common frames omitted

I was trying to modify the original request to mark it unconsumed, but it seems not allowed...

I am wondering if there's still any other way to work around this? Thanks in advance.

Roy Ling
  • 1,592
  • 1
  • 14
  • 31

0 Answers0