简体   繁体   中英

Spring application/x-www-form-urlencoded prevent encoding of string

We have a @PostMapping which is a webhook provided to a third party, which is called whenever an event occurs on the third party.

@PostMapping(path = "/some-api", consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public void proessWebhook(@RequestBody String request) {

The request strings keys are encoded in the String request . For our internal requirements, we need the key to not be pre-encoded when spring sets it to the String request variable. We tried URL decoding the variable, however, the problem is that there are values that we don't want to decode, so decoding the entire string decodes the key and the values.

An example if in the CURL request the input is some key=some%20value , the request variable would be set to some%20key=some%20value . Whereas we only need the key decoded.

On top of this, the order of the parameters also change from the input, it is important for the order to be the same.

How do we go about doing this?

if your method parameter is of a type MultiValueMap, you can use either the @RequestParam or @RequestBody annotation to bind it appropriately with the body of the HTTP request.

@PostMapping(
path = "/some-api",
consumes = {MediaType.APPLICATION_FORM_URLENCODED_VALUE})
public ResponseEntity<String> funName(
@RequestParam MultiValueMap<String,String> paramMap) throws Exception {
}

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