Skip to content

UriUtils does double encode [SPR-8507] #13153

Closed

Description

@spring-projects-issues

Eduard Dudar opened SPR-8507 and commented

I'm seeing quite strange behavior and I'm not sure why UriUtils works this way.

In detail I'm having URL that I want to ask using RestTemplate. Url is the following [CODE]API=http://server/path?key=...&url={url}[/CODE] where {url} is variable that is passed from the outside. Url that is passed in can be any valid URL with query string or not. Then I call [CODE]this.rest.getForObject(API, String.class, url)[/CODE] that call API mentioned above and passed incoming URL into.

And here is what I see. When I pass unencoded url into REST call UriUtils does nothing so I get more parameters then I need because own url parameters are just appended to API parameters. When I do manual encoding first I get double encoded parameter that is also invalid.

I found the code and it definitely works that way, but I'm not sure why already encoded parameters are forced to be double encoded.


Affects: 3.0.5

Reference URL: http://forum.springsource.org/showthread.php?111473-UriUtils-does-double-encode

Issue Links:

Activity

  1. spring-projects-issues commented on Jul 5, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Rossen - this looks at least loosely related to #13050.

  2. spring-projects-issues commented on Jul 27, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Rossen Stoyanchev commented

    There is a good reason for this behavior.

    When the RestTemplate is invoked using one of the methods accepting a String URI template, the characters '&', '=', and '+' are not encoded since they are legal URI characters. It wouldn't be possible to know which ones should be encoded and which ones shouldn't.

    Consider the following where the value of "url" is "url=http://host/path?a=b&b=c":

    http://server/path?key=foo&{url}
    

    As for the double encoding, the javadoc of the RestTemplate is very clear that the URI template is expected to not be encoded or else it will be double-encoded.

    You can use the RestTemplate methods that accept a URI instead. See the recommendation here:
    https://jira.springsource.org/browse/SPR-5516?focusedCommentId=58444&page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#comment-58444

    You can also watch or comment under #13050 for a potential improvement.

  3. added
    type: bug
    status: declined
    in: web
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: webstatus: declined

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions