qqilihq
qqilihq

Reputation: 11454

RESTfully checking for resource availability

Assume, I have a REST resource with users and I need to check, whether a username already exists or not (and thus can be registered on the front end).

From my feeling, a clean RESTful approach for doing so would be to use a HEAD request on the username to check, such as:

HEAD /api/users/mary

And returning either a status 200 (username exists), or 404 (username does not exist). The problem I'm facing now: I'm performing a REST request via JavaScript, whether a user name already exists, like so:

$http.head('/api/users/' + username)
     .success(function(data, status, headers, config) {
         // username is taken
     })
     .error(function(data, status, headers, config) {
         // username is free
     });

Now, Safari browser (and supposedly other browsers as well), are logging an error, in case a non-existing user it hit (i.e. 404 is returned from API):

Failed to load resource: the server responded with a status of 404 (Not Found)

Although a normal end user would usually not see the console, I've been thinking whether my approach in general is acceptable, or I should go for a different solution. Another idea I came up with is to use a 204 status (No Content) instead of the 404, but that feels less 'explicit'.

Any different advice or suggestions welcome!

Upvotes: 1

Views: 462

Answers (1)

daniel.brubeck
daniel.brubeck

Reputation: 654

I think that you should leave the 404 error for HEAD /api/users/mary, but if you want to avoid the console log, you could create a different service that returns a resource that tells you if a the user exists or not.

Upvotes: 1

Related Questions