How to respond to an alternate URI in a RESTful web service

I am creating a RESTful web service that has multiple URIs for one of its resources because there is more than one unique identifier. Should the server respond to a GET request for an alternate URI by returning a resource, or should I redirect HTTP 3xx to a canonical URI? Is HTTP 303 (see also) the most appropriate redirect?

Clarification: The HTTP specification makes it clear that the choice of redirection depends on what future URI requests should use. In my application, the "canonical" URI is the most stable of the alternatives; the alternate URI will always refer to the same canonical URI or become invalid.

+4
source share
3 answers

I personally would like to return the resource, not redirect it, although I suspect that it is only because my subconscious mind says that redirects are slower.

However, if you decide to use redirection, I think 302 or 307 might be more suitable than 303, although w3.org contains information about the different forwarding codes that you could use.

+4
source

The W3C section "Architexture of the World Wide Web", Volume 1, has a section on URI aliases ( Section 2.3.1 ) that states the following:

“When a URI alias becomes a common currency, the URI owner must use protocol methods such as server-side redirects to link the two resources. The community takes advantage when the URI owner supports redirecting URI aliases to the corresponding“ official URI. For more information about redirection, see Section 10.3 “Redirection” in RFC2616 . See Also CHIPS for a discussion of some guidelines for server administrators.

For what it's worth, I would recommend a 302 redirect.

+2
source

The answer from Ubiguchi was what I needed, except that now I think that redirection is the way, following the link to the HTTP 1.1 section, where the response codes are indicated. Turns out I really need a 301 redirect because the URI I am redirecting to is more "correct" and stable, so it should be used for future queries.

+1
source

Source: https://habr.com/ru/post/1276464/


All Articles