简体   繁体   中英

Adding Codec to Add Content-Length in my Requests in WebClient

I want to add some sort of ChannelOutboundHandler to my WebClient that takes the request, add the ' Content-Length ' header to it, and forwards it. I know that the class HttpContentEncoder does this. How do I configure this class in my WebClient ?

If you just want to add Content-Length , you do not need any handler. You can do it like this:

String requestBody = "something";
String response =
        WebClient.create()
                .post()
                .uri("https://postman-echo.com/post")
                .contentLength(9)
                .body(BodyInserters.fromObject(requestBody))
                .retrieve()
                .bodyToMono(String.class)
                .block();
System.out.println(response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM