7

I have a Spring-Boot (1.5.3) application running on a Tomcat 8.5. Within it, i have a RestController with a single parameter.

This parameter is decoded automatically - which is fine for all other controllers within my application, but for this one, i need the raw request data, since they will be used within another request. Please note that i wold like to only disable the auto-decoding for this Controller/request and keep the normal behaviour everywhere else.

@RestController @RequestMapping("/rest/test") public class TestController { @RequestMapping("/test") public ResponseEntity<String> test(@RequestParam final String test){ return ResponseEntity.ok("Requested: " + test); } } 

Now, if i send access it with

curl localhost/rest/test/test?test=%C3%B6%20%C3%A4 

I receive the output:

Requested: ö ä 

I would like to have

Requested: %C3%B6%20%C3%A4 

Any Ideas?

4 Answers 4

7

I had the similar issue with a parameter which had the base64Encoded value, but for me only the + sign was getting converted into space. If I would have used URLEncoder.encode() method, other special characters were also getting converted. So, I solved this by passing the param in RequestBody instead of RequestParam.

PS : This is not a perfect answer for this question. I am just writing this answer for anyone who get the similar problem like this and land to this thread.

Sign up to request clarification or add additional context in comments.

1 Comment

You'll want to use the URL safe alphabet for base64 encoding when including values in URLs: tools.ietf.org/html/rfc4648#section-5 This is available in the JDK as of Java 8.
2

If you need this behavior only in a single place, you can encode the parameter back to the original form using the standard Java URLEncoder inside of the controller body:

@RequestMapping("/test") public ResponseEntity<String> test(@RequestParam final String test) { String encodedParam = URLEncoder.encode(test, "UTF-8"); return ResponseEntity.ok("Requested: " + encodedParam); } 

3 Comments

Hi Daniel, Thank you, but this is a workaround i would like to avoid since it should be possible to avoid the en/decoding anywhay
Please be aware, that the Java URLEncoder and URLDecoder are not the right tools to handle URLs... as is written in the corresponding docs: "Utility class for HTML form encoding. This class contains static methods for converting a String to the application/x-www-form-urlencoded MIME format. For more information about HTML form encoding, consult the HTML specification." This is not about URLs at all. It is wrong to use it to encode or decode entire URLs.
In fact, you are representing an HTML form in the query portion of the URL. The URL spec doesn't specify the contents of the query. That is "Scheme dependent". HTTP spec, however, makes no mention. It is only in the HTML specs that the application/x-www-form-urlencoded MIME type is defined. See tools.ietf.org/html/rfc3986#section-3.4 and w3.org/TR/html401/interact/forms.html. That said, that particular Java implementation is out of date, but they don't update it because there are so many callers that would get broken in subtle ways. So, tread carefully ...
1

I ended up ensuring, that the received data was did not contain invalid characters.

private static final String VALID_URL_CHARACTER_PATTERN = "^[A-Za-z0-9-._~:/?#\\[\\]@!$&'()*+,;=`.]+$"; private static final String ensureEncoding(final Object obj) { if (obj == null) { return null; } String val = obj.toString(); if (!val.matches(VALID_URL_CHARACTER_PATTERN)) { try { val = URLEncoder.encode(val, "UTF-8"); } catch (UnsupportedEncodingException e) { log.error("Unexpected encoding Problem on value: " + val, e); } } return val; } 

Comments

0

From the Spring team https://github.com/spring-projects/spring-framework/issues/21577

URI uri = fromUriString(format("http://localhost:%s/%s", serverPort, "/myEndPoint")) .queryParam("param1", param1) .queryParam("base64Param", "{base64Param}") .build(base64Param); 

This syntax will interpret special characters in the URL correctly and avoid the need for any additional encoding/decoding.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.