URL Design
One of the most important things to remember about building a REST URL is that each URL must identify one resource. In general, this means that URLs are usually broken into parts:
- Lists of top-level objects:
/users and /photos - Top Level Instance Instance:
/users/1/Dani and /photos/4356 - Instance level lists:
/users/1/Dani/friends - Dani's Friends/users/1/Dani/photos - Dani's Photos
Resource Interaction
Interactions with these resources (i.e., Create, read, update, delete) are handled through "HTTP verbs" or " HTTP methods ", which each URL is actually called. The advantage of this is that each resource (or “thing”) you only need to know how to do 4 different things, which means that you have a much simpler application.
Your application is also much more structured and divided, which simplifies testing and allows you to update and make changes to it much easier, because things are more loosely coupled.
Once you don't have a clear 1-1 connection between a single resource and a single URL, you no longer have RESTful URLs. After you start inserting action strings into the query string, you are essentially making remote procedure calls (RPC), not REST. Everything passing through a central point brings things together more than they should be, making your architecture tough, hard to change, and very difficult to test.
Search
The trick is that for any type of resource such as a list, you can have this list as a result of some query. It says nothing that the list should always be the same. It would be inappropriate to use http://www.example.com/Dani/friends?long=1&lat=2&field=photos , because this will return to you a list of photos of Dani's friends who are quite far from Dani User.
Since you are looking for photos, and we already have a URL that identifies the “Photos List” resource, that is, a URL that we must use, but just to get these photos with certain attributes.
So, for your example search of all photos belonging to a specific user (which may be one of Dani's friends, you can do something like:
GET /photos?owner=[userId]
and you can only search for photographs taken within 1 km of some lat / long coordinate:
GET /photos?owner=[userId]&radius=1&lat=[someLat]&long=[someLong]
Or, if you look a little wider, maybe you need photos of all Dani's friends from this area:
GET /photos?ownerFriendOf=[Dani userId]&radius=1&lat=[someLat]&long=[someLong]
In all of these cases, you are viewing the list of photos based on the query string that you send to the list of photos in /photos .
Caching
Caching is just an added bonus. Theoretically, any request can be cached, but you do not need to worry about it right now. In general, however, if you stick to the REST architecture, you'll be fine when the time comes.
Security
HTTP has a number of built-in ways to solve the security problem, and any of them will work, depending on how secure your application is. Security Basic and Digest are well suited for connecting applications with applications, as they send their authentication tokens (i.e. username and password) along with the request. However, for the security flow that is associated with the user, you most likely want to use the session mechanism and use the HTTP Cookie headers to track the session.
In all cases, however, at any time when the username / password is moved from the client to the server, it must be associated with a secure SSL connection (https) so that attackers do not sniff it. For particularly sensitive applications, all interaction can be through an SSL connection, and for other applications there can only be an login sequence. In general, however, being safer is better than less.
Ease of implementation
On the security front, most web frameworks have built-in methods that can handle all the security methods I have described. You might be wondering if you need to use a web framework, and although this is not a strictly required requirement, it will significantly reduce the amount of work that you have to do, and at the same time it will reduce the number of errors, as most of the “heavy lifting” processed by the frame and has been very well tested.
Many frameworks today have built-in support for RESTful request processing, and you can get up and running quickly. RPC-based support is often less supported because it does not have a well-defined application architecture such as REST, but it is still possible in almost any environment.
In the long run, however, you are likely to get a much better chance for your dollar by going with RESTful architecture.