Skip to content

How to handle Request Form Data? #97

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
iNitialM505 opened this issue Dec 25, 2023 · 1 comment
Open

How to handle Request Form Data? #97

iNitialM505 opened this issue Dec 25, 2023 · 1 comment
Assignees
Labels
question Further information is requested

Comments

@iNitialM505
Copy link

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

@piomin
Copy link
Owner

piomin commented Mar 19, 2024

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

@piomin piomin added the question Further information is requested label Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants