1. You've got the right idea about how to design your resources, IMHO. I wouldn't change a thing.
2. Rather than trying to extend HTTP with more verbs, consider what your proposed verbs can be reduced to in terms of the basic HTTP methods and resources. For example, instead of an activate_login
verb, you could set up resources like: /api/users/1/login/active
which is a simple boolean. To activate a login, just PUT
a document there that says 'true' or 1 or whatever. To deactivate, PUT
a document there that is empty or says 0 or false.
Similarly, to change or set passwords, just do PUT
s to /api/users/1/password
.
Whenever you need to add something (like a credit) think in terms of POST
s. For example, you could do a POST
to a resource like /api/users/1/credits
with a body containing the number of credits to add. A PUT
on the same resource could be used to overwrite the value rather than add. A POST
with a negative number in the body would subtract, and so on.
3. I'd strongly advise against extending the basic HTTP status codes. If you can't find one that matches your situation exactly, pick the closest one and put the error details in the response body. Also, remember that HTTP headers are extensible; your application can define all the custom headers that you like. One application that I worked on, for example, could return a 404 Not Found
under multiple circumstances. Rather than making the client parse the response body for the reason, we just added a new header, X-Status-Extended
, which contained our proprietary status code extensions. So you might see a response like:
HTTP/1.1 404 Not Found
X-Status-Extended: 404.3 More Specific Error Here
That way a HTTP client like a web browser will still know what to do with the regular 404 code, and a more sophisticated HTTP client can choose to look at the X-Status-Extended
header for more specific information.
4. For authentication, I recommend using HTTP authentication if you can. But IMHO there's nothing wrong with using cookie-based authentication if that's easier for you.