Search code examples
javaspringrestspring-bootspecial-characters

Missing entire string in query parameter when given special character - Java Spring Boot


I'm currently writing a Java Spring Boot application with a REST API server. I'm cutting out a lot of non-relevant code here, but one of my POST routes looks like this:

@RequestMapping(value = "/post-request", method = RequestMethod.POST)
public String postRequest(
        @RequestParam(name = REQUEST_STRING) String requestString,
        ){ logger.info(requestString)}

This approach works great for most strings, but I'm getting a really weird error when including special characters in the request parameter. For example, if I send the application with a request containing

This (&) is an ampersand

within Postman, the application will log

This (

and cut out everything else after the special character. I'm fairly certain that this has to do with the way that special characters are encoded within REST API's, especially when it comes to request parameters, but what am I doing wrong here? Is there a way for me to get the full string using Java Spring Boot, or am I restricted from doing this?


Solution

  • RequestParam are diffrentiated with "&" so if you pass "&" directly in string anything after & will be treated as another RequestParam

    eg:

    http://testdata.com?username=test&password=hello&123&type=user

    Here password will be passed as hello and 123 will be treated as anotehr RequestParam

    To overcome this you need to do encoding for "&" as "%26"

    http://testdata.com?username=test&password=hello%26123&type=user