简体   繁体   中英

RESTful - Different URI of same resource to get different forms of same resource

I'm pushed into a peculiar situation where I'm not able to decide what is wrong and what is right.

I've a resource called Invoice. To get a JSON or XML representation I use below URI

somedomain.com/inovices/{InvoiceNumber} - Invoice number is numeric
Accept: application/xml

When I want a PDF of same resource I use below URI:

somedomain.com/inovices/{InvoiceNumber} - Invoice number is numeric
Accept: application/pdf

Both the above url's are served for authenticated requests. We also want to support same resource using a GUID for unauthenticated requests, hence we want to use below URL

somedomain.com/inovices/{GUID}
Accept: application/pdf

Above URL is like a permanent URL and anybody can access this URL any number of times. My confusion is whether providing URL as above is RESTful or not. Because in one URL I'm using invoice number which is numeric and for permanent URL I'm replacing it with GUID.

Reason why I felt this is wrong is same resource is represented with two different URI's (number and GUID) even though they are returning same resource. Or is it just my assumption that it is wrong? Is it against any REST cosntraint is what I'm not able to understand?

There's no problem at all different URIs to point at the same resource. It's not only ok, sometimes it's also recommended, if that would add value to the user.

Think of these examples:

GET /api/users/543
GET /api/users/bob-marley

or, as SO does:

GET /questions/16637720/restful-different-uri-of-same-resource-to-get-different-forms-of-same-resource
GET /q/16637720/1118323

There are similar examples everywhere. You may want to add this "unnessecary" information if it's going to help users, or SEO , and still keep the short version available. Or imagine senarios, where you would want to add more ways of accessing resources without breaking existing ones. It sounds quite common to me and you don't break any rules by having more than one URI for the same resource.

If you are worried that a user might think that the two resources are different since she used different URI, you can have one URI redirect to the other, to make it explicit that it's exactly the same resource (as SO does when you hit the short link, or the link without the thread heading).

Here is a relevant answer .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM