Readable debug logging for http requests with spring webclient

人盡茶涼 提交于 2019-12-04 07:36:09

You can do it with doOnNext(), if you use DataBuffer as your reader:

public Mono<ServerResponse> selectByPost(ServerRequest request) {
  Flux<DataBuffer> requestBodyFlux = request.bodyToFlux(DataBuffer.class)
    .doOnNext(dataBuffer -> {
      if (debug ) {
        log.debug(new String(dataBuffer.asByteBuffer().array()));
      }
      Scannable.from(dataBuffer).tags().forEach(System.out::println);
    });
}

This is probably not the best way to do it, it would of course be a nice feature, if netty would provide different ways of logging the payload. Hex does have its benefits, depending on what you need to debug.

This is a very subjective question.

You don't find that format readable/useful, but I do think the opposite for a few reasons. Hexadecimal is really useful here since HTTP can be tricky:

  • it's not always easy to read special chars in logs (character encoding, etc)
  • servers/clients can interpret/parse headers in different ways
  • it's really hard to track down issues when HTTP control characters are involved without that

It all boils down to really seing what's sent/received over the network, which is usually what you should see if you're looking into a particular issue.

But I agree that this level of detail should not be the first and only available information for debugging. There should be an intermediate level where you can get the basics about the HTTP exchanges without looking at raw hex data.

For more on that, please follow the dedicated issue on Reactor Netty.

Seems like the responding server is returning gzipped content, so it makes sense that you're not able to read it.

If you really want to intercept at the raw HTTP level, ensure your request header does not specify it can accept GZipped content (accept-encoding: gzip).

Another alternative may be to log the request at another layer, when it's already been unzipped from the raw data stream, but not yet processed by your application code - not sure how this would work in Reactive webclient though ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!