REST, or Representational State Transfer, is an architectural style for distributed systems defining certain constraints that provide efficiency, scalabilty, and other desirable attributes.
REST was introduced by Roy Fielding. The constraints he identified were:
REST is an architectural style, NOT a product, NOT an API.
The World Wide Web is an example of a RESTful system.
A RESTful system is characterized by seven principles.
http://www.yourcompany.com/products/2443
http://www.yourcompany.com/getProduct?id=2443
Hopefully you will get the chance to write a REST API yourself!
Here are four guidelines that can help you do a good job.
Remember when OOP was new? You had to shift your thinking from procedures to objects (from verbs to nouns): QueryOptimizer, ImageMapper, SiteInitializer, StringTokenizer, Enumerator, StreamReader, StreamWriter, Factory. Transitioning from RPC to REST is the same thing!
So ask yourself the question:
What are my resources?
Example answers: products, campaigns, customers, listings, profiles, lists, reviews, ads.
Resources do not have to be finely-grained. They do not have to correspond exactly to database tables.
Finally, you will have do deal with limited number of verbs. Hpw do you map your actions to these verbs?
Think in terms of nouns. It’s that simple.
RESTful:
http://example.com/profiles http://example.com/profiles?what=pizza&where=90069&page=4&rpp=20 http://example.com/profiles/2734 http://example.com//twitter-handles/jp http://example.com/providers/2345/budgets/4567
Non-RESTful:
http://example.com/order-system?op=deleteCustomer&id=2 http://example.com/find-events?where=90069&after=2010-05-20 http://example.com/api/products/update?id=3&name=mouse
If you are writing a webapp back-end, you most certainly are using some kind of server-side product like Node for JavaScript, Flask for Python, Spring for Java, Rails for Ruby, or plain old PHP. Many of these products have a mechanism for making REST APIs easily. For Java, you can use a JAX-RS implementation such as Jersey or RestEasy.
Let’s learn with an example.
Request | Meaning |
---|---|
GET /things | Get all, or some of, the things. Use query parameters to restrict the set (i.e., do a search!), or to paginate. |
PUT /things | Replace the set of all things. Put entire set in payload. |
POST /things | Create a new thing. Server sets id. Put representation of new thing in payload. |
DELETE /things | Delete all of the things. |
GET /things/8 | Get thing 8. |
PUT /things/8 | Replace thing 8 with new thing, but id stays 8. Put representation in payload. Or create a new thing with id 8. |
PATCH /things/8 | Edit a part of thing 8. Payload can have a description of what to change, or maybe just the fields needing changing. |
DELETE /things/8 | Delete thing 8. |
You can implement OPTIONS yourself, too. HEAD will pretty much be handled by a framework.
Respect method constraints:
Request headers further refine the request. See Wikipedia’s list of headers for more information.
Use response codes as they were intended to be used. The ones you should be aware of (that is, that you may set yourself as part of your application) are:
Code | Meaning |
---|---|
400 | Bad Request |
401 | Authorization Required |
403 | Forbidden |
404 | Not Found |
405 | Method Not Allowed (service doesn't support the requested method at that URI) |
406 | Not Acceptable (server can't give back a representation in a requested format) |
415 | Unsupported Media Type (server can't process the request body) |
200 | OK |
201 | Created (usually you should set the Location header for this) |
202 | Accepted (used for asynch requests) |
204 | No Content |
301 | Moved Permanently |
409 | Conflict |
410 | Gone |
Response headers contain additional information about the response. See Wikipedia’s list of headers for more information.
Start with these if you are new:
Ready for something more hardcore?
Here’s an old but good presentation:
Here are some questions useful for your spaced repetition learning. Many of the answers are not found on this page. Some will have popped up in lecture. Others will require you to do your own research.
GET /employees/8
POST /employees
PUT /employees/8
We’ve covered: