javaurlurl-encodingurldecode

URLDecoder is converting '+' into space


I have a hash key in one of my query params which can have + char with other special chars. The issue is when this URL is getting decoded URLDecoder converts + char into space. Is there a way we can enforce URLDecoder not to convert '+' into space.


Solution

  • According to HTML URL Encoding Reference:

    URLs cannot contain spaces. URL encoding normally replaces a space with a plus (+) sign or with %20.

    and + sign itself must be encoded with %2B. So if you want to pass your hash as a GET parameter in URL, you should replace plus signs with %2B in your hash. Do not replace every + in the entire URL because you might ruin other string parameters which are supposed to contain spaces.