Readable debug logging for http requests with spring webclient

后端 未结 3 1008
死守一世寂寞
死守一世寂寞 2021-02-15 12:31

I\'m using Spring reactive WebClient for sending requests to a http server. Inorder to view the underlying request & response that\'s being sent, I enabled debug logging for

相关标签:
3条回答
  • 2021-02-15 13:10

    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 ;)

    0 讨论(0)
  • 2021-02-15 13:11

    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.

    0 讨论(0)
  • 2021-02-15 13:26

    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.

    0 讨论(0)
提交回复
热议问题