Monday, April 21, 2008

Spaces in REST URLs

So I need to deal with spaces in REST URL's. Something like:

.../timescale/Late Jurassic

I'd rather not allow these types of URL's (since I don't think the spec even allows them) and was thinking about the programming and also community practices issues this raises. If a person wants a space they likely should use "%20", however this just doesn't look good and I believe gets confusing.

I could simply allow the community to create such URL's even though I believe them wrong and then attempt to resolve them in my Grails application with:

someString.replaceAll("%20", " ")

since firefox and I suspect other browsers will convert the spaces to "%20" for the user.

This seems a poor approach though since if someone puts two spaces into the URL or somehow a tab gets in things get ugly fast. A person just has far too many bad issues to deal with.

Forcing the community to use camel case (here I will use lower camel case but one could use upper camel case) would require a URL like:

.../timescale/lateJurasic

where I could split the parameter in Groovy with:

someString.replaceAll("([A-Z])", " $1").trim()

so "lateJurassic" becomes "late Jurassic" which is what is in my database (actually "Late Jurassic" but I can case insensitive search) since that is the "proper" form of the name.

This puts the burden of creating a camel case representation of "Late Jurassic" on the client end of the application (ie, make this someone else's problem, which is always a good thing).

I suspect this is just a case where the best resource identifier in the database is not always the best resource identifier for the REST URL representation. It would seem then that apply a best practice approach one would use a camel case representation. This might be a situation where using upper camel case is better yet since the proper cases on the name is "Late Jurassic" resulting in a URL:

.../timescale/LateJurassic

That is still fine since the .trim() call will remove the leading space still in the above code. I'd love to hear other thoughts on such mappings.

take care
Doug

3 comments:

Joshua Jacobs said...

What about using a plus sign for spaces? I think this is what delicious uses: http://del.icio.us/jmandala/grails+groovy

fils said...

Joshua,
Anything is possible I guess as a deliminator. I guess my thought on all this is that I was not able to say "the community best practice is X". Perhaps there isn't one, given the nature of REST services. Or perhaps I just didn't find it. Given the focus on web architecture aspects of REST I guess URI best practices are used. However, those don't address the issue of mapping between data stores and service representations that don't conform the standards of the the other (or visa versa)

Unknown said...

You won't mistake a delimited word for the name of a controller. For example if you had LateJurassicController it would have a URI like lateJurassic as well... but it would never have a URI like Late+Jurrassic