我正在设计一个 Web 应用程序,然后停下来思考我的 API 应该如何设计成一个 RESTful Web 服务。目前,我的大多数 URI 都是通用的,可能适用于各种 Web 应用程序:
GET /logout // destroys session and redirects to /
GET /login // gets the webpage that has the login form
POST /login // authenticates credentials against database and either redirects home with a new session or redirects back to /login
GET /register // gets the webpage that has the registration form
POST /register // records the entered information into database as a new /user/xxx
GET /user/xxx // gets and renders current user data in a profile view
POST /user/xxx // updates new information about user
我有一种感觉,我在这里做了很多错误的东西后,在所以和谷歌左右。
从 /logout开始,可能因为我实际上没有 GET任何东西-它可能更适合于 POST请求 /logout,破坏会话,然后 GET的重定向。那么 /logout这个术语应该保留吗?
那 /login和 /register呢。我可以将 /register改为 /registration,但这并不能从根本上改变我的服务的工作方式——如果它有更深层次的问题的话。
现在我注意到,我从未公开 /user资源。也许可以利用这一点。例如,以用户 myUser为例:
foo.com/user/myUser
或者
foo.com/user
最终用户不需要在 URI 中额外的详细信息。然而,哪一个在视觉上更吸引人呢?
关于 REST 业务,我在这里注意到了一些其他的问题,但是如果可能的话,我真的希望能够得到一些关于我在这里所阐述的内容的指导。
谢谢!
更新:
我还想就以下问题发表一些意见:
/user/1
对
/user/myUserName