fstanis
fstanis

Reputation: 5554

HTTP status code for "never going to exist"

I'm building a service where resources are divided into categories, e.g. example.com/category/foo would represent "foo" (think: similar to SO tags).

New categories may be added at any time and, when attempting to view a category that doesn't yet exist, users would be able to suggest it added.

However, I'd like to just outright ban some category names (e.g. NSFW terms). This means that (assuming "bar" is one such name) example.com/category/bar not only never existed, but is guaranteed never to exist in the future.

Which HTTP status code is appropriate for this situation?

Several ideas come to mind:

  1. 410 Gone - while this makes it clear the resource will not be available in the future, I'm not sure if it's appropriate as it also seems to imply it used to exist in the past.

  2. 400 Bad Request - the request is technically not malformed, so this is probably not the way to go.

  3. 404 Not Found initially seemed like the logical option, but doesn't convey the permanence of the ban, especially since I plan to use 404 for categories that don't yet exist, but can be suggested.

  4. 301 Moved Permanently and redirect to another page, either the home page or some other page explaining that some category names are banned.

Upvotes: 1

Views: 252

Answers (2)

user3417917
user3417917

Reputation: 196

I would suggest a 410 Gone is an appropriate response, assuming that your web clients are common and implement the http spec correctly (most popular ones do).

When looking on this page here: Status Code Definitions

The section about 410 gone says:

10.4.11 410 Gone

The requested resource is no longer available at the server and no forwarding address is known. This condition is expected to be considered permanent. Clients with link editing capabilities SHOULD delete references to the Request-URI after user approval. If the server does not know, or has no facility to determine, whether or not the condition is permanent, the status code 404 (Not Found) SHOULD be used instead. This response is cacheable unless indicated otherwise.

The 410 response is primarily intended to assist the task of web maintenance by notifying the recipient that the resource is intentionally unavailable and that the server owners desire that remote links to that resource be removed. Such an event is common for limited-time, promotional services and for resources belonging to individuals no longer working at the server's site. It is not necessary to mark all permanently unavailable resources as "gone" or to keep the mark for any length of time -- that is left to the discretion of the server owner.

I take that to mean the resource definitely does not exist now and will not exist in the future, giving the permanence that you want, and it is also cachable unless stated otherwise.

Usually I would not need to know that it had "never existed before", I just know that it doesn't exist right now when i'm requesting information about it, and it is not going to exist in the future if i request it again. If I did need to learn that certain categories never existed in the past (e.g. a blacklist of categories), i would probably want a separate explicit request that I could use to get all of those categories rather than having to check each category one at a time at run time.

Upvotes: 0

ibtarek
ibtarek

Reputation: 795

I don't think there's such a thing as "will never exist" in the specs of HTTP. Let's think about it slightly differenly :

  • the resource is created through your website (and only through your website). If a resource is created, that means that whatever validation you put in place succeeded.

So to keep it simple stupid, you should stick to HTTP semantics. If someone hits a URL : example.com/category/{cat}, either you know {cat} (it's in your DB and has a valid name, right?) and process the request safley, or you have never seen {cat} before and you just return 404.

After all there's an infinity of possible values that one could use for {cat} and all of them would be valid URLS.

Hope it helps

Upvotes: 2

Related Questions