如何使用Apache HttpClient记录未解压的gzip内容

huangapple go评论74阅读模式
英文:

How to log unzipped gzip content using Apache HttpClient

问题

我使用Spring Boot(版本3.1.1)的RestTemplate,它配置为使用Apache的org.apache.hc.client5.http.classic.HttpClient(版本5.2.1),如下所示:

@Bean
public RestTemplate restTemplate() {
    final RestTemplate restTemplate = new RestTemplate();
    // 我们使用HTTP客户端创建Rest Template以启用日志记录
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
    return restTemplate;
}

我已经按照以下方式启用了Logback日志记录:

<logger name="org.apache.hc.client5.http" level="debug"/>

当我发送请求后,检查日志时,我可以看到响应已被gzip压缩,因此传入的有效载荷不可读:

... 省略的标头
2023-06-180 10:23:54.222 [Test worker] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; "AY[0xffffffce][0x19][0xffffffd5][0xffffffc6][0xffffff8f]hFY[0x3]H[0xffffff9e]3[0xffffff8a][0x1][0xfffffffa]*{[0xffffff9f]V[0xffffffe3]|[0xffffffba][0xffffffb5]O'[0x1e][0xffffffcb][0xffffffff]w[0xffffffc1][0xffffffe7][0x7f][0xfffffffe][0x1f][0x0][0x0][0x0][0xffffffff][0xffffffff][0x3][0x0][0xffffffcd][0xffffff89][0xfffffff0][0xffffffd7][0x3][0xffffffd5][0x3][0x0][\r][\n]"

是否有可能在解压缩后记录响应正文?

英文:

I'm using Spring Boot (ver. 3.1.1) RestTemplate which is configured to use Apache org.apache.hc.client5.http.classic.HttpClient (ver. 5.2.1) like this

@Bean
public RestTemplate restTemplate() {
    final RestTemplate restTemplate = new RestTemplate();
    //we make Rest Template using HTTP client to be able to turn logging on
    restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
    return restTemplate;
}

I have turned the Logback logging like this

&lt;logger name=&quot;org.apache.hc.client5.http&quot; level=&quot;debug&quot;/&gt;

And when I check the logs after I send a request, I can see the response is being compressed by gzip, so the incoming payload is unreadable:

... headers omitted for brevity
2023-06-180 10:23:54.222 [Test worker] DEBUG org.apache.hc.client5.http.wire - http-outgoing-0 &lt;&lt; &quot;AY[0xffffffce][0x19][0xffffffd5][0xffffffc6][0xffffff8f]hFY[0x3]H[0xffffff9e]3[0xffffff8a][0x1][0xfffffffa]*{[0xffffff9f]V[0xffffffe3]|[0xffffffba][0xffffffb5]O&#39;[0x1e][0xffffffcb][0xffffffff]w[0xffffffc1][0xffffffe7][0x7f][0xfffffffe][0x1f][0x0][0x0][0x0][0xffffffff][0xffffffff][0x3][0x0][0xffffffcd][0xffffff89][0xfffffff0][0xffffffd7][0x3][0xffffffd5][0x3][0x0][\r][\n]&quot;

Is it possible to log the response body after it is unzipped?

答案1

得分: 1

Apache HTTP客户端不会对发送/接收的数据进行日志记录。这就是为什么它被称为"wire logging"。

如果你想要记录未压缩的有效负载,你需要在你的代码中自己实现。

英文:

No. Apache HTTP client will log the data as it is sent/received. That's why it is called wire logging.

If you want to log the payload un compressed you have to do it yourself in your code.

huangapple
  • 本文由 发表于 2023年6月29日 16:50:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76579495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定