1

I'm new with RESTful and I'm trying to develop an API as flexible as possible but I'm a bit confused about resource associations.

According to many tutorials a common RESTful resource association look like:

/dogs
/dogs/{id}
/owners/{owid}/dogs

But what about an association such as:

/owners/{owid}/dogs/{id}

Is it relevant or is it useless (since an id is supposed to be unique so there's no need to specify the owner)?

DanMan
  • 11,323
  • 4
  • 40
  • 61
Duddy67
  • 836
  • 2
  • 10
  • 26
  • possible duplicate of [RESTful API URI Design](http://stackoverflow.com/questions/13038943/restful-api-uri-design) – DanMan Jan 09 '15 at 14:52

2 Answers2

3

Go for the following setup instead.

/dogs
/dogs/{dogid}
/people
/people/{personid}

GET /people/7
{
    "dogs": [
        "/dogs/3",
        "/dogs/5"
    ]
}

GET /dogs/3
{
    "owners": [
        "/people/7",
        "/people/2"
    ]
}

Notice how this no longer ties you to the concepts of "Every dog has exactly one owner" and "Every person owns dogs" (by virtue of being referred to as an "owner" in your original design).

In general, nesting URIs as if they were a folder structure is a bad practice. It will tie you to conceptual relationships you might not want permanently.

Timothy Shields
  • 75,459
  • 18
  • 120
  • 173
2

For sure it is relevant when dogs have identifiers within the scope of an owner e.g. the nickname that an owner uses for his dog.

But you could also use a/the global identifier for {id} and have the advantage at execution time for this particular path that the server would ensure that this particular dog is actually owned by {owid}.

In any case it is not against REST rules to have multiple paths leading up to the same resource. It is good practice to return a single canonical URL as a part of the response.

I find http://blog.2partsmagic.com/restful-uri-design/ a good read on RESTful design.

Hans Z.
  • 50,496
  • 12
  • 102
  • 115