Spring webflux webfilter removes the body from the request

Viewed 434

I have a filter created where I access the body of the payload and do some logic on it (for now let's say I log the body). In the last step, I return Mono but when the request proceeds through the controller to services it throws a bad request error that the body is missing.

Code for the filter:

    public Mono<Void> filter(ServerWebExchange serverWebExchange, WebFilterChain webFilterChain) {
        HttpHeaders headers = serverWebExchange.getRequest().getHeaders();
        String domain = headers.getFirst("domain");
        return serverWebExchange.getRequest().getBody()
                .single()
                .flatMap(body -> Mono.just(parseBody(body)))
                .flatMap(s -> webFilterChain.filter(serverWebExchange));
}

private String parseBody(DataBuffer fbody) {
        System.out.println("parsing body");
        ByteArrayOutputStream baos = null;
        try {
            baos = new ByteArrayOutputStream();
            Channels.newChannel(baos).write(fbody.asByteBuffer().asReadOnlyBuffer());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return baos.toString(StandardCharsets.UTF_8);
}

The error: org.springframework.web.server.ServerWebInputException: 400 BAD_REQUEST "Request body is missing"

What can cause this behavior?

1 Answers
Related