views:

88

answers:

3

What should I use:

  • /findby/name/{first}_{last}
  • /findby/name/{first}-{last}
  • /findby/name/{first};{last}
  • /findby/name/first/{first}/last/{last}

etc.

The URI represents a Person resource with 1 name, but I need to logically separate the first from the last to identify each. I kind of like the last example because I can do:

  • /findby/name/first/{first}
  • /findby/name/last/{last}
  • /findby/name/first/{first}/last/{last}
+2  A: 

You could always just accept spaces :-) (querystring escaped as %20)

But my preference is to just use dashes (-) ... looks nicer in the URL. unless you have a need to be able to essentially query in which case the last example is better as you noted

Joel Martinez
+1  A: 

I like using "_" because it is the most similar character to space that keeps the URL readable.

However, the URLs you provided don't seem really RESTful. A URL should represent a resource, but in your case it represents a search query. So I would do something like this:

/people/{first}_{last}
/people/{first}_{last}_(2)  - in case there are duplicate names

It this case you have to store the slug ({first}_{last}, {first}_{last}_(2)) for each user record. Another option to prepend the ID, so you don't have to bother with slugs:

/people/{id}-{first}_{last}

And for search you can use non-RESTful URLs:

/people/search?last={last}&first={first}

These would display a list of search results while the URLs above the page for a particular person.

I don't think there is any use of making the search URLs RESTful, users will most likely want to share links to a certain person's page and not search result pages. As for the search engines, avoid having the same content for multiple URLs, and you should even deny indexing of your search result pages in robots.txt

Leventix
There is no such thing as a RESTful URL. The contents/structure of an URL is opaque to a RESTful system. The results of a search is a resource too.
Darrel Miller
+1  A: 

For searching:

/people/search?first={first}&last={last}
/people/search?first=george&last=washington

For resource paths:

/people/{id}-{first}-{last}
/people/35-george-washington

If you are using Ruby on Rails v3 in standard configuration, here's how you can do it.

# set up the /people/{param} piece
# config/routes.rb
My::Application.routes.draw do
  resources :people
end

# set up that {param} should be {id}-{first}-{last}
# app/models/person.rb
class Person < ActiveRecord::Base
  def to_param
    "#{id}-#{to_slug(first_name)}-#{to_slug(last_name)}"
  end
end

Note that your suggestion, /findby/name/first/{first}/last/{last}, is not restful. It does not name resources and it does not name them succinctly.

Justice
Which REST constraint does that last URL violate?
Darrel Miller
I am not aware that there is a checklist which defines what it means to be REST.
Justice
@Justice Yes there are. The REST architectural style was defined by applying a set of constraints to the null style. Here http://nordsc.com/ext/classification_of_http_based_apis.html is a good table for identifying if a system is restful or not.
Darrel Miller