How to compress the body of Spring WebClient post request?

时光总嘲笑我的痴心妄想 提交于 2021-01-06 04:15:14

问题


I am doing some testing with Spring WebClient. In the following codes, I have set the compress to true. However when I check the debug logs, I can see the "accept-encoding: gzip" header is added, but the body is not compressed. Any way to force compress on post request body? Thanks.

HttpClient httpClient = HttpClient.create().compress(true).wiretap(true);

WebClient webClient = WebClient.builder()
                .baseUrl("https://jsonplaceholder.typicode.com")
                .defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
                .defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
                .clientConnector(new ReactorClientHttpConnector(httpClient))
                .build();

Post post = new Post();

        post.setUserId(1000L);
        post.setId(2000L);
        post.setTitle("Reactor Netty");

        StringBuffer sb = new StringBuffer();
        IntStream.range(1, 1000).forEach(i -> sb.append("Spring boot webclient"));
        post.setBody(sb.toString());

        Post p = webClient.post().uri("/posts").syncBody(post).retrieve().bodyToMono(Post.class).block();

回答1:


Please refer to this.

.compress(true) is for the server to response as gzip. That's why your post body is not compressed.

I am not familiar with manual http-client and web-client. When I use Spring, I use RestTemplate. This answer details how to force zipping your request body.



来源:https://stackoverflow.com/questions/57568018/how-to-compress-the-body-of-spring-webclient-post-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!