spring-boot-logging icon indicating copy to clipboard operation
spring-boot-logging copied to clipboard

How to handle Request Form Data?

Open iNitialM505 opened this issue 2 years ago • 1 comments

When I using this package, I've no issue about Request body from json, but I got some issue when Request Form Data

When I send a request with form data, the data displays in string form

I have tried to override RequestLoggingInterceptor, but data still flows into RequestLoggingInterceptor

public class RequestLogging extends RequestLoggingInterceptor {

    private static final Logger LOGGER = LoggerFactory.getLogger(RequestLoggingInterceptor.class);
    public RequestLogging(ServerHttpRequest delegate, boolean logHeaders, String requestId) {
        super(delegate, logHeaders, requestId);
    }

    @Override
    public Flux<DataBuffer> getBody() {
        MediaType contentType = getDelegate().getHeaders().getContentType();
        boolean isFormData = contentType != null && contentType.isCompatibleWith(MediaType.MULTIPART_FORM_DATA);
        boolean isJson = contentType != null && contentType.isCompatibleWith(MediaType.APPLICATION_JSON);
        if (isFormData) {
            LOGGER.info("Request: method={}, uri={}, payload={}, audit={}", getDelegate().getMethod(),
                    getDelegate().getPath(), "[Form Data]", StructuredArguments.value("audit", true));
            return super.getBody();
        } else if (isJson) {
            return super.getBody().publishOn(Schedulers.boundedElastic()).map(dataBuffer -> {
                byte[] bytes = new byte[dataBuffer.readableByteCount()];
                dataBuffer.read(bytes);
                DataBufferUtils.release(dataBuffer);

                String body = new String(bytes, StandardCharsets.UTF_8);
                LOGGER.info("Request: method={}, uri={}, payload={}, audit={}", getDelegate().getMethod(),
                        getDelegate().getPath(), body, StructuredArguments.value("audit", true));
                return dataBuffer;
            });
        } else {
            LOGGER.info("Request: method={}, uri={}, unsupported content type={}, audit={}", getDelegate().getMethod(),
                    getDelegate().getPath(), contentType, StructuredArguments.value("audit", true));
            return super.getBody();
        }
    }
}

image

iNitialM505 avatar Dec 25 '23 13:12 iNitialM505

Can you paste your request? What would you exactly like to see in the logs for your request?

piomin avatar Mar 19 '24 23:03 piomin