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:
- Upload new parameters
- Get the latest parameters
- Get parameters for a given business date
- Make a set of parameters active
- Validate a set of parameters
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:
- POST where you include the parameter file in the post request
- GET of first URL
- GET of second URL
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
??
The design of your urls has nothing to do with whether your application is RESTful or not. the phrase "RESTful URLS" is therefore nonsense.
I think you should do some more reading on what REST actually is. REST treats the URLS as opaque, and as such doesn't know what's in them, whether theres verbs or nouns or whatever. You might still want to design your URLS, but that's about UI, not REST.
That said, lets get to your question: The last two cases are not RESTful, and don't fit into any kind of restful scheme. Those are what you might call RPC. If you're serious about REST you'll have to rethink how your application works from the ground up. Either that, or abandon REST and just do your app as an RPC app.
Hrmmm maybe not.
The idea here is that you have to treat everything as a resource, so once a set of parameters has a URL you can refer to it from, you just add
get [parametersurl]/validationresults
post [paramatersurl]
body: {command:"activate"}
but again, that activate thing is RPC, not REST.
Perhaps something like:
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.
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 sent202 Accepted
after performing an operation successfully or creating a resource asynchronously400 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 exceptions401 Unauthorized
when someone accesses your API either without supplying a necessaryAuthorization
header or when the credentials within theAuthorization
are invalid; don't use this response code if you aren't expecting credentials via anAuthorization
header.403 Forbidden
when someone accesses your API in a way that might be malicious or if they aren't authorized405 Method Not Allowed
when someone uses POST when they should have used PUT, etc413 Request Entity Too Large
when someone attempts to send you an unacceptably large file418 I'm a teapot
when attempting to brew coffee with a teapotETag
headers are good when you can easily reduce a resource to a hash valueLast-Modified
should indicate to you that keeping around a timestamp of when resources are updated is a good ideaCache-Control
andExpires
should be given sensible valuesIf-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.I would suggest the following Meta resource and methods.
Make parameters active and/or validate them:
Check if the parameters are active and valid:
Edit: Indeed the URI would have prevented
GET
requests from remaining idempotent.For the validation however, the use of HTTP status codes to notify the validity of a request (to create a new or modify an existing 'parameter') would fit a Restful model.
Report back with a
400 Bad Request
status code if the data submitted is/are invalid and the request must be altered before being resubmitted (HTTP/1.1 Status Codes).This relies on validating at submission time though, rather than deferring it as in your use-case. The other answers have suitable solutions to that scenario.