0

i have following code (package org.springframework.web.util)

UriComponentsBuilder.fromUriString("/test/").queryParam("rt","http://a.com?u=1").build(false).encode().toUriString()

actual result: /test/?rt=http://a.com?u%3D1

what i expected is: /test/?rt=http%3a%2f%2fa.com%3fu%3d1

any idea?

juncai
  • 53
  • 1
  • 4

1 Answers1

0

from what I understand, UriComponentsBuilder doesn't encode the query parameters automatically, just the original HttpUrl it's instantiated with. In other words, you still have to explicitly encode:

UriComponentsBuilder is encoding your URI in accordance with RFC 3986 (see http://www.ietf.org/rfc/rfc3986.txt, in particular section 3.4 which is about the 'query' component of a URI).

Within the query component, the characters '/' and ':' are permitted, and do not need escaping.

To take the '/' character for example: the query component (which is clearly delimited by unescaped '?' and (optionally) '#' characters), is not hierarchical and the '/' character has no special meaning. So it doesn't need encoding.

UriComponentsBuilder.fromUriString("/test/").queryParam("rt",URLEncoder.encode("http://a.com?u=1", "UTF-8")).build(false).encode().toUriString();

output : /test/?rt=http%253A%252F%252Fa.com%253Fu%253D1

Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
  • thanks. but you can see, UriComponentBuilder still does something like "encoding". It changes '=' to '%3D'. – juncai Aug 14 '14 at 02:27