Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring REST URL Encoding Scheme: %20 or + Which one?

Tags:

rest

url

spring

I made a Spring REST application where you can perform CRUD operations based on HTTP methods of POST, PUT, GET, DELETE. I have the typical URI template of

http://host/root/{id}/{name}/{address} and etc.

We have a client who is accessing this REST service. Apparently they are sending parameters for multi-word name and address in the following form:

http://host/root/11/John+Smith/10+Las+Vegas+USA

They are using the HTML encoding scheme based on application/x-www-form-urlencoded type. According to the article in Wikipedia

The application/x-www-form-urlencoded type

The encoding used by default is based on a very early version of the general URI percent-encoding rules, with a number of modifications such as newline normalization and replacing spaces with "+" instead of "%20". - http://en.wikipedia.org/wiki/Percent-encoding

However it appears the standard URL encoding scheme is to use %20 in replacing spaces in URI templates. Which one is correct?

My Spring REST automatically converts %20 to spaces. It's interpreted correctly. I'm using Spring 3.0.4. When + is met by my REST service, it's accepted as is. Of course when I put validation to exclude +, it is indeed excluded as expected.

Am I within standards or are there such double standards? Or is the client using an ancient scheme?

like image 636
chris Avatar asked Nov 13 '10 03:11

chris


People also ask

How do I know if a URL is encoded?

So you can test if the string contains a colon, if not, urldecode it, and if that string contains a colon, the original string was url encoded, if not, check if the strings are different and if so, urldecode again and if not, it is not a valid URI. You can make this loop simpler if you know what schemes you can expect.

Why do we need to encode URL?

Why do we need to encode? URLs can only have certain characters from the standard 128 character ASCII set. Reserved characters that do not belong to this set must be encoded. This means that we need to encode these characters when passing into a URL.

What is URL encoding in java?

Simply put, URL encoding translates special characters from the URL to a representation that adheres to the spec and can be correctly understood and interpreted.


1 Answers

The point is that application/x-www-form-urlencoded can be used only in request parameters, whereas percent encoding is also supported in a path.

So,

http://host/root/11/?name=John+Smith&address=10+Las+Vegas+USA

is fine and will be properly decoded by Spring MVC, but

http://host/root/11/John+Smith/10+Las+Vegas+USA

is wrong and Spring MVC doesn't decode it, because the following form should be used instead:

http://host/root/11/John%20Smith/10%20Las%20Vegas%20USA
like image 71
axtavt Avatar answered Nov 13 '22 18:11

axtavt