Reputation: 36664
Is it a best practice of using URIs in JSON (or XML) representation of REST resources, for example
For example for a resource which has a list of attachments, where every attachment has an id which can be used to retrieve it using an URL like http://myserver.com/resources/attachments/:
{
fileName: "screenshot.png"
contentType: "application/octet-stream"
id: 52004
}
Should I also add an uri element like
{
fileName: "screenshot.png"
contentType: "application/octet-stream"
id: 52004
uri: /resources/attachments/52004
}
Upvotes: 5
Views: 871
Reputation: 245
Yes, I think you should include a link to each item in the collection. An API is not RESTful (and more importantly, not as useful) without the links. If you think a human client would rather have a link than instructions on how to request an item by ID, the same applies to a non-human client. You should also give the client some idea of how the item relates to the current resource by providing a link relationship:
link : { uri: "/resources/attachments/52004", rel: "/rels/file-attachment" }
John
Upvotes: 8
Reputation: 15109
If would be quite useless, since you would still need to send the information from the server if the user requested the resource and you'd fetch the resource from it's id rather than it's URI.
Also, sometimes some resources don't require a URI cause they are never going to be requested or can't be directly requested.
Upvotes: 0