How to create REST URLs without verbs?
I'm struggling to determine how to design restful URLs. I'm all for the restful approach of using URLs with nouns and not verbs don't understand how to do this.
We are creating a service to implement a financial calculator. The calculator takes a bunch of parameters that we will upload via a CSV file. The use cases would involve:
I gather the restful approach would be to have the following type URLs:
/parameters
/parameters/12-23-2009
You could achieve the first three use cases with:
But how do you do the 4th and 5th use case without a verb? Wouldn't you need URLs like:
/parameters/ID/activate
/parameters/ID/validate
??
也许是这样的:
PUT /parameters/activation HTTP/1.1
Content-Type: application/json; encoding=UTF-8
Content-Length: 18
{ "active": true }
General principles for good URI design:
/resource
or /resource/
; create 301 redirects from the one you don't use (Note: I did not say "RESTful URI design"; URIs are essentially opaque in REST.)
General principles for HTTP method choice:
General principles of web service design with HTTP:
201 Created
after creating a resource; resource must exist at the time the response is sent 202 Accepted
after performing an operation successfully or creating a resource asynchronously 400 Bad Request
when someone does an operation on data that's clearly bogus; for your application this could be a validation error; generally reserve 500 for uncaught exceptions 401 Unauthorized
when someone accesses your API either without supplying a necessary Authorization
header or when the credentials within the Authorization
are invalid; don't use this response code if you aren't expecting credentials via an Authorization
header. 403 Forbidden
when someone accesses your API in a way that might be malicious or if they aren't authorized 405 Method Not Allowed
when someone uses POST when they should have used PUT, etc 413 Request Entity Too Large
when someone attempts to send you an unacceptably large file 418 I'm a teapot
when attempting to brew coffee with a teapot ETag
headers are good when you can easily reduce a resource to a hash value Last-Modified
should indicate to you that keeping around a timestamp of when resources are updated is a good idea Cache-Control
and Expires
should be given sensible values If-None-Modified
, If-Modified-Since
) With regard to your specific question, POST should be used for #4 and #5. These operations fall under the "RPC-like" guideline above. For #5, remember that POST does not necessarily have to use Content-Type: application/x-www-form-urlencoded
. This could just as easily be a JSON or CSV payload.
Whenever it looks like you need a new verb, think about turning that verb into a noun instead. For example, turn 'activate' into 'activation', and 'validate' into 'validation'.
But just from what you've written I'd say your application has much bigger problems.
Any time a resource called 'parameter' is proposed, it should send up red flags in every project team member's mind. 'parameter' can literally apply to any resource; it's not specific enough.
What exactly does a 'parameter' represent? Probably a number of different things, each of which should have a separate resource dedicated to it.
Another way to get at this - when you discuss your application with end users (those who presumably know little about programming) what are the words they themselves use repeatedly?
Those are the words you should be designing your application around.
If you haven't yet had this conversion with prospective users - stop everything right now and don't write another line of code until you do! Only then will your team have an idea of what needs to be built.
I know nothing about financial software, but if I had to guess, I'd say some of the resources might go by names such as "Report", "Payment", "Transfer", and "Currency".
There are a number of good books on this part of the software design process. Two I can recommend are Domain Driven Design and Analysis Patterns.
链接地址: http://www.djcxy.com/p/7000.html下一篇: 如何创建没有动词的REST URL?