0

I am trying to intercept all incoming HTTP requests and process the body attached to these requests in my Spring MVC (not Spring Boot) app. To implement this "inbound-interceptor", I am using Spring's HandlerInterceptor interface. Once the request is intercepted, I am trying to retrieve the body as follows:

ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
Map<String, String[]> params = requestWrapper.getParameterMap();
byte[] body = requestWrapper.getContentAsByteArray();

Referring to this article, the limitations of trying to extract the body this way are:

  1. Content-type of the request must be x-www-form-urlencoded
  2. Method-type must be POST

For the application I am building, I cannot enforce either of these constraints as the calls come from heterogeneous sources beyond my control. Is there some way to override this behavior to allow extraction of the body for requests not supported by default? Or, alternatively, is there another approach to performing this task?

P.S. I am performing logging + some custom processing on the body. So solutions such as the ones mentioned in this answer are not too helpful

Tabish Mir
  • 717
  • 6
  • 26

1 Answers1

1

Have you tried Logbook? https://github.com/zalando/logbook Works with pure Spring.

Their Default Log Writer looks promising: https://github.com/zalando/logbook/blob/main/logbook-core/src/main/java/org/zalando/logbook/DefaultHttpLogWriter.java

And you may just want to extend this class to log to all Loggers you want. You can even do something completely different with the request besides logging.

Sven Döring
  • 3,927
  • 2
  • 14
  • 17
  • Thanks for the suggestion, will give it a try. Although for now, I have found a combination of `OncePerRequestFilter` and `ContentCachingWrappers` that works just fine to capture everything required from the request (https://github.com/taabishm2/Spring-Inbound-Interceptor) without requiring any dedicated libraries to be used – Tabish Mir Nov 06 '20 at 12:37