英文:
Apache HttpClient : Retry with failsafe results in 400 (bad request)
问题
以下是翻译好的内容:
我正在使用Apache的HttpClient
与Failsafe Java库。以下是伪代码的示例:
RetryPolicy<CloseableHttpResponse> policy = new RetryPolicy<>()
.handleResultIf(/* 响应代码为404 */)
.withMaxRetries(5)
.withDelay(Duration.ofSeconds(10));
CloseableHttpResponse response = Failsafe.with(policy).get(() -> httpClient.execute(myRequest));
它在localhost
上调用一个测试端点,并且我已经模拟它执行以下操作:
- 前3个请求返回404
- 第4个请求返回200
现在,当我执行上述代码时,我看到以下行为:
- HttpClient发送
get
请求,结果为404 - 由于响应是
404
,重试策略开始工作并重新尝试请求 - 重试的请求以
400
失败,未能实际到达代理 - 所有后续的重试都以
400
失败。响应没有任何主体
我期望步骤2中的请求能够命中我的模拟,然而它在未命中的情况下失败。HttpClient
是否缓存响应或尝试阻止后续的重试?
英文:
I am using Apache HttpClient
with Failsafe java library. Below is how the (pseudo) code looks like:
RetryPolicy<CloseableHttpResponse> policy = new RetryPolicy<>()
.handleResultIf(/* Response code is 404 */)
.withMaxRetries(5)
.withDelay(Duration.ofSeconds(10));
CloseableHttpResponse response = Failsafe.with(policy).get(() -> httpClient.execute(myRequest));
It's calling a test endpoint at localhost
and I have mocked it to do the following:
- Return 404 for the first 3 requests
- Return 200 for the 4th request
Now, when I execute the above code, I see the following behavior:
- HttpClient sends
get
request, it results in 404 - As the response is
404
, retry policy kicks in and retries the request - Retried request fails with
400
without actually reaching the proxy - All the subsequent retries fail with
400
. The response doesn't have any body
I expect the request in step 2 to hit my mock, however, it fails without hitting it. Does HttpClient
cache the response or tries to prevent the subsequent retries?
答案1
得分: 0
显然,在调用httpClient
的execute
方法之前,我是使用addHeader
方法在请求中设置了头部。这导致请求中出现了重复的Content-Type
和Authorization
头部。
由于这些头部值肯定是无效的,所以请求在未能命中URL之前就产生了400
错误。
英文:
Apparently, I was setting headers in the request with addHeader
method before calling execute
for httpClient
. This resulted in requests with duplicate Content-Type
and Authorization
headers.
As these header values are certainly invalid, the requests resulted in 400
error without hitting the url.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论