I've read the closed issue https://github.com/spring-projects/spring-framework/issues/24262 but did not get the reply. The problem is that ExchangeFilterFunction.ofRequestProcessor ClientRequset.body is BodyInserter<?, ? super ClientHttpRequest>,

But I need to fetch the body as a String/JSON.

Basically, I managed to access the body using ClientHttpRequestDecorator, but headers are empty.

public class SigningClientHttpRequestDecorator extends ClientHttpRequestDecorator {

    public SigningClientHttpRequestDecorator(ClientHttpRequest delegate) {
        super(delegate);
    }

    @Override
    public Mono<Void> writeWith(Publisher<? extends DataBuffer> body) {
        Mono<DataBuffer> buffer = Mono.from(body);
        return super.writeWith(buffer.doOnNext(dataBuffer -> {
            try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
                Channels.newChannel(baos).write(dataBuffer.asByteBuffer().asReadOnlyBuffer());
                Instant now = Instant.now();
                String encodedSign = SignUtil.signRequest(now, baos.toByteArray());
                super.getHeaders().set("testHeader", encodedSign);
            } catch (Exception e) {
                e.printStackTrace();
                throw new BlackboxResponseFilterException("Error applying filter to response");
            }
        }));
    }
}
public class SignClientHttpConnectorDecorator implements ClientHttpConnector {

    private final ClientHttpConnector delegate;

    public SignClientHttpConnectorDecorator(ClientHttpConnector delegate) {
        this.delegate = delegate;
    }

    @Override
    public Mono<ClientHttpResponse> connect(HttpMethod httpMethod,
                                            URI uri,
                                            Function<? super ClientHttpRequest, Mono<Void>> callback) {
        return this.delegate.connect(httpMethod, uri, clientHttpRequest -> callback.apply(new SigningClientHttpRequestDecorator(clientHttpRequest)));
    }
}

I am trying to set custom header, but super.getHeaders() returns empty result and they are read only, how can I set headers ? Here is how I set connector

HttpClient client = HttpClient.create();
//do some ssl stuff with client
ReactorClientHttpConnector connector = new ReactorClientHttpConnector(client);
        return WebClient.builder().clientConnector(new SignClientHttpConnectorDecorator(connector)).build();

Comment From: rstoyanchev

Thanks for getting in touch, but it feels like this is a question that would be better suited to Stack Overflow. As mentioned in the guidelines for contributing, we prefer to use the issue tracker only for bugs and enhancements. Feel free to update this issue with a link to the re-posted question (so that other people can find it) or add some more details if you feel this is a genuine bug.

Generally you should be able to "see" headers but you shouldn't be setting headers in the writing phase since at that point the response should already be committed.

Comment From: dalaiRama06

@toxakktl did you find a solution to this? I am trying to perform a similar action where I will sign the body and add the signature as a header but I am unable to find any reference on how to do so apart from this. Any help would be great

Comment From: IEnoobong

hey @dalaiRama06 @toxakktl please did you find a solution to this?