Location>code7788 >text

How to crack the request body if WebClient cannot obtain?

Popularity:900 ℃/2025-02-10 08:55:00

At first during development, I encountered a problem that bothered me: When I converted the object to JSON, the result I got was not consistent with the request body that was eventually passed to the third-party interface. This problem caused me to have an error when performing encryption operations, because the encryption process is based on the request body, and the request body is affected by different factors. Therefore, I want to get the request body directly inside the class for encryption.

However, after reviewing the information, it was found that WebClient did not directly provide an interface to obtain the request body. Even if I force this function, the process will be quite cumbersome. After some careful consideration, I decided to use the curb to save the country to solve this problem. Next, I will share in detail how I bypassed this difficulty and solved the problem smoothly.

Break the deadline

First of all, the root cause of the problem lies in the inconsistent JSON conversion results of the request body of both parties, which leads to an error during the encryption process. According to logic, since the encryption is based on the request body and the format of the request body has already encountered problems during conversion, can I directly pass the JSON data I have converted to a third-party interface?

In this way, there is no need to worry about the reasons and details of the inconsistency in the conversion process of the request body between the two parties, and avoid the troubles caused by various differences.

MultiValueMap<String, String> jsonContentHeaders = (HunYuanConstants.DEFAULT_CHAT_ACTION,chatRequest);
        return ().uri("/").headers(headers -> {
                (jsonContentHeaders);
            })
            .bodyValue((chatRequest))
//            .body((chatRequest), )
            .retrieve()
            .bodyToFlux();

What I used in the code beforebody((chatRequest), )method where an object is passed, but there are problems doing so. Later I adjusted the code and changed it to directly use the JSON method during encryption to convert the data into JSON format and pass it on, which perfectly solved the problem.

Some people may want to read the request body, but they just want to print the data. Here you can use printing directly and pass the data over. If the requirement is not limited to printing the request body, but also wants to obtain some additional information, such as data in the request header, then in this case, you can consider using a filter to implement it. The specific implementation method is as follows:

ExchangeFilterFunction filter = (request -> {
     //Print head and cookies
     ("Request Headers: {}", ());
     ("Request Cookies: {}", ());
     return (request);
 });

  = ().baseUrl(baseUrl).filter(filter).defaultHeaders(jsonContentHeaders).build();

This method itself cannot directly print the request body parameters, because its scope of action is mainly limited to processing the requested metadata.

If you are still obsessed with how to obtain the request body method, you might as well take a look here. The solution is really difficult. I didn't try it myself, the address is as follows:/questions/45240005/how-to-log-request-and-response-bodies-in-spring-webflux

At first I relied on the traditional way to handle the request body, but eventually I found that these methods were not applicable. After repeated thinking and adjustment, I decided to adopt a more flexible approach to directly pass the converted JSON data, which successfully bypassed the original problem. In the process of solving this problem, facing complex technical difficulties, relying solely on conventional tools and methods is not enough to solve the problem efficiently. It is crucial to try different solutions more often.


I am working hard Xiaoyu, a serious Java Northeast server development, and I think about the secrets of AI technology all day long. I love to communicate with others and I like to share my experience with everyone. He also became a Tencent Cloud Creation Star, an Alibaba Cloud expert blogger, Huawei Cloud Enjoy Expert, and an outstanding author of gold nuggets. I also got various essay contests and open source competitions.

💡 I want to share all the detours and experiences I have taken on the road of technology, and give you some inspiration to your learning and growth, and help me.

🌟 Welcome to follow the hard-working Xiaoyu, let’s make progress together! 🌟