RESTful URI design

I'm looking for a right way to represent searches as a RESTful uri.

I have two models: countries and states, where countries have a collection the states.

What's the right way to represent a search for countries with id or name attributes?

api/countries/1/id

api/countries/Italy/name

or

api/countries?id=1

api/countries?name=Italy

Also, if I want view the list of states?

api/countries/1/id/states

api/countries/Italy/name/states

Thanks


For these two models ie. countries and states , I think following Uri patterns will be most appropriate.

  • All Countries : api/country
  • Specific Country : api/country/{England}
  • All States : api/country/{England}/State
  • Specific State : api/country/{England}/State/{Berkshire}
  • in above example value inside {} represents the variables.


    It depends on your tastes. Both variant are ok.

    For states it's possible to create new endpoint

    api/states/name/Italy
    

    Your approach, using name in the REST URL is wrong. The name is simply an attribute of the country not a sub-resource, meaning you should not see it in the URL. An option would be though, to use it as key and in this case, the name's value could be used in place of the ID.

    api/countries/Italy
    

    I think what you are trying to achieve though, is to filter fields, ie get only the names:

    api/countries/Italy/states?fields=name
    

    or, if you predefine some formats

    api/countries/Italy/states?format=onlyName
    
    链接地址: http://www.djcxy.com/p/40958.html

    上一篇: 用于高级过滤静默API结果的URL设计

    下一篇: RESTful URI设计