开发者

find resource by unique attribute

I'm developing a simple REST API:

  • json
  • mostly CRUD-like operations
  • POST for create, PUT for update

Each resource is identified by a unique database handle (in practice an auto increment), which is exposed through the URI like this:

 /collection/131

The representation of a collection is a list of child resources, like this:

{
  "children": [
    { "name": "foo", "href": "/collection/131" }
  ]
}

the "name" field must be unique throughout the collection.

I'm quite pleased with this pattern, except for one thing; to find a resource with a given name you have iterate through the collection representation.

How would one best add a "resolve name to URI" mechanism, and still stay true to REST? I see the following options:

  1. put the name in the URI
  2. make the collection searchable
  3. some kind of redirect or lookup mechanism

The reason I do not want to do (1) is that the 开发者_C百科URI address space is not part of the API but an implementation detail. The only known resource is the root resource, from which all other resources can be found.

How can I make it simpler for the API users to resolve a name into a URI ?


This is one of the problems with REST. There aren't nearly enough HTTP methods to support common operations.

Some may argue this is not the proper REST way to do this, but I can't see how else you could do it:

/collection/findByName?name=...

Edit: You could also opt for a more generic method too which you could eventually extend:

/collection/search?name=...

/collection/search?name=...&something=...

Edit (2): What about: /collection?name=...

I believe in the end it really boils down to what you think is more intuitive.

  • Christian
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜