Skip to content

Commit 933e982

Browse files
committed
Updated restquicktips.html and httpstatuscodes.html
1 parent 033fa31 commit 933e982

File tree

3 files changed

+86
-20
lines changed

3 files changed

+86
-20
lines changed

httpstatuscodes.html

+5-5
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ <h3>Wikipedia</h3>
147147
<p>The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.</p>
148148
<h3>Wikipedia</h3>
149149
<p>The server successfully processed the request, but is not returning any content.</p>
150-
<p><i class="icon-star"></i> Status when wrapped responses (e.g. JSEND) are not used and nothing is in the body (e.g. DELETE).</p>
150+
<p><i class="icon-star"></i> Indicates success but nothing is in the response body, often used for DELETE and UPDATE operations.</p>
151151
</div>
152152
</div>
153153
<div class="span4">
@@ -270,7 +270,7 @@ <h3>Wikipedia</h3>
270270
</div>
271271
</div>
272272
<div class="span4">
273-
<i class="icon-star"></i> <a data-toggle="collapse" data-target="#not_modified" href="#">304 Not Modified</a>
273+
<a data-toggle="collapse" data-target="#not_modified" href="#">304 Not Modified</a>
274274
<div id="not_modified" class="collapse">
275275
<p>If the client has performed a conditional GET request and access is allowed, but the document has not been modified, the server SHOULD respond with this status code. The 304 response MUST NOT contain a message-body, and thus is always terminated by the first empty line after the header fields.</p>
276276
<p>The response MUST include the following header fields:</p>
@@ -287,7 +287,6 @@ <h3>Wikipedia</h3>
287287
<p>If a cache uses a received 304 response to update a cache entry, the cache MUST update the entry to reflect any new field values given in the response.</p>
288288
<h3>Wikipedia</h3>
289289
<p>Indicates the resource has not been modified since last requested. Typically, the HTTP client provides a header like the If-Modified-Since header to provide a time against which to compare. Using this saves bandwidth and reprocessing on both the server and client, as only the header data must be sent and received in comparison to the entirety of the page being re-processed by the server, then sent again using more bandwidth of the server and client.</p>
290-
<p><i class="icon-star"></i> Used for conditional GET calls to reduce band-width usage. If used, must set the Date, Content-Location, ETag headers to what they would have been on a regular GET call. There must be no body on the response.</p>
291290
</div>
292291
</div>
293292
<div class="span4">
@@ -386,11 +385,12 @@ <h3>Wikipedia</h3>
386385
</div>
387386
</div>
388387
<div class="span4">
389-
<a data-toggle="collapse" data-target="#method_no_allowed" href="#">405 Method Not Allowed</a>
388+
<i class="icon-star"></i> <a data-toggle="collapse" data-target="#method_no_allowed" href="#">405 Method Not Allowed</a>
390389
<div id="method_no_allowed" class="collapse">
391390
<p>The method specified in the Request-Line is not allowed for the resource identified by the Request-URI. The response MUST include an Allow header containing a list of valid methods for the requested resource.</p>
392391
<h3>Wikipedia</h3>
393392
<p>A request was made of a resource using a request method not supported by that resource; for example, using GET on a form which requires data to be presented via POST, or using PUT on a read-only resource.</p>
393+
<p><i class="icon-star"></i> Used to indicate that the requested URL exists, but the requested HTTP method is not applicable. For example, <em>POST /users/12345</em> where the API doesn't support creation of resources this way (with a provided ID). The Allow HTTP header must be set when returning a 405 to indicate the HTTP methods that are supported. In the previous case, the header would look like &quot;Allow: GET, PUT, DELETE&quot;</p>
394394
</div>
395395
</div>
396396
</div>
@@ -651,7 +651,7 @@ <h3>Wikipedia</h3>
651651
<p>The server encountered an unexpected condition which prevented it from fulfilling the request.</p>
652652
<h3>Wikipedia</h3>
653653
<p>A generic error message, given when no more specific message is suitable.</p>
654-
<p><i class="icon-star"></i> The general catch-all error when the server-side throws an exception.</p>
654+
<p><i class="icon-star"></i> The general catch-all error when the server-side throws an exception. Use this only for errors that the consumer cannot address from their end&mdash;never return this intentionally.</p>
655655
</div>
656656
</div>
657657
<div class="span4">

lessons/restquicktips.html

+78-12
Original file line numberDiff line numberDiff line change
@@ -52,21 +52,87 @@ <h1>REST API Quick Tips</h1>
5252
</div>
5353
<div class="row">
5454
<div class="span12">
55-
<p>Whether it's technically RESTful or not (according to the six constraints mentioned above), here are a few recommended REST-like concepts that will result in better, more usable services:</p>
55+
<p>Whether it's technically RESTful or not (according to the six constraints mentioned previously), here are a few recommended REST-like concepts. These six quick tips will result in better, more usable services.</p>
5656
<h2>Use HTTP Verbs to Mean Something</h2>
57-
<p>Any API consumer is capable of sending GET, POST, PUT, and DELETE verbs, and they greatly enhance the clarity of what a given request does. Also, GET requests must not change any underlying resource data. Measurements and tracking may still occur, which updates data, but not resource data identified by the URI.</p>
58-
<h2>Sensible Resource Names</h2>
59-
<p>Having sensible resource names or paths (e.g., /posts/23 instead of /api?type=posts&amp;id=23) improves the clarity of what a given request does. Using URL query-string parameters is fantastic for filtering, but not for resource names.</p>
60-
<p>Appropriate resource names provide context for a service request, increasing understandability of the service API. Resources are viewed hierarchically via their URI names, offering consumers a friendly, easily-understood hierarchy of resources to leverage in their applications.</p>
61-
<p>Resource names should be nouns—avoid verbs as resource names. It makes things more clear. Use the HTTP methods to specify the verb portion of the request.</p>
62-
<h2>XML and JSON</h2>
63-
<p>Favor JSON support, but unless the costs of offering both JSON and XML are staggering, offer them both. Ideally, let consumers switch between them by just changing an extension from .xml to .json. In addition, for supporting AJAX-style user interfaces, a wrapped response is very helpful. Provide a wrapped response, either by default or for separate extensions, such as .wjson and .wxml to indicate the client is requesting a wrapped JSON or XML response.</p>
64-
<p>JSON in regards to a "standard" has very few requirements. And those requirements are only syntactical in nature, not about content format or layout. In other words, the JSON response to a REST service call is very much part of the contract—not described in a standard. More about the JSON data format can be found at <a href="http://www.json.org/">http://www.json.org/</a>.</p>
65-
<p>Regarding XML use in REST services, XML standards and conventions are really not in play other than to utilize syntactically correct tags and text. In particular, namespaces are not, nor should they be use in a RESTful service context. XML that is returned is more JSON like—simple and easy to read, without the schema and namespace details present—just data and links. If it ends up being more complex than this, see the first paragraph for this tip&mdash;the cost of XML will be staggering. In our experience nobody uses the XML responses anyway. It's just too expensive to process.</p>
57+
<p>API consumers are capable of sending GET, POST, PUT, and DELETE verbs, and these verbs greatly enhance the clarity of what a given request does. Also, GET requests must not change any underlying resource data. Measurements and tracking may still occur, which updates data, but not resource data identified by the URI.</p>
58+
<p>Generally, the four primary HTTP verb are used as follows:
59+
<dl>
60+
<dt>GET</dt>
61+
<dd>Read a specific resource (by an identifier) or a collection of resources.</dd>
62+
<dt>PUT</dt>
63+
<dd>Update a specific resource (by an identifier) or a collection of resources. Can also be used to create a specific resource if the resource identifier is know before-hand.</dd>
64+
<dt>DELETE</dt>
65+
<dd>Remove/delete a specific resource by an identifier.</dd>
66+
<dt>POST</dt>
67+
<dd>Create a new resource. Also a catch-all verb for operations that don't fit into the other categories.</dd>
68+
</dl>
69+
</p>
70+
<h2>Provide Sensible Resource Names</h2>
71+
<p>Producing a great API is 80% art and 20% science. Creating a URL hierarchy representing sensible resources is the art part. Having sensible resource names (which are just URL paths, such as /customers/12345/orders) improves the clarity of what a given request does.</p>
72+
<p>Appropriate resource names provide context for a service request, increasing understandability of the API. Resources are viewed hierarchically via their URI names, offering consumers a friendly, easily-understood hierarchy of resources to leverage in their applications.</p>
73+
<p>Here are some quick-hit rules for URL path (resource name) design:
74+
<ul>
75+
<li>Use identifiers in your URLs instead of in the query-string. Using URL query-string parameters is fantastic for filtering, but not for resource names.
76+
<ul><li><strong>Good:</strong> /users/12345</li><li><strong>Poor:</strong> /api?type=user&amp;id=23</li></ul></li>
77+
<li>Leverage the hierarchical nature of the URL to imply structure.</li>
78+
<li>Design for your clients, not for your data.</li>
79+
<li>Resource names should be nouns. Avoid verbs as resource names. It makes things more clear. Use the HTTP methods to specify the verb portion of the request.</li>
80+
<li>Use plurals in URL segments to keep your API URIs consistent across all HTTP methods, using the collection metaphor.
81+
<ul><li><strong>Recommended:</strong> /customers/33245/orders/8769/lineitems/1</li><li><strong>Not:</strong> /customer/33245/order/8769/lineitem/1</li></ul></li>
82+
<li>Avoid using collection verbage in URLs. For example 'customer_list' as a resource. Use pluralization to indicate the collection metaphor (e.g. customers vs. customer_list).</li>
83+
<li>Use lower-case in URL segments, separating words with underscores ('_') or hyphens ('-'). Some servers ignore case so it's best to be clear.</li>
84+
<li>Keep URLs as short as possible, with as few segments as makes sense.</li>
85+
</ul>
86+
</p>
87+
<h2>Use HTTP Response Codes to Indicate Status</h2>
88+
<p>Response status codes are part of the HTTP specification. There are quite a number of them to address the most common situations. In the spirit of having our RESTful services embrace the HTTP specification, our Web APIs should return relevant HTTP status codes. For example, when a resource is successfully created (e.g. from a POST request), the API should return HTTP status code 201. A list of valid <a href="http://www.restapitutorial.com/httpstatuscodes.html">HTTP status codes</a> is available <a href="http://www.restapitutorial.com/httpstatuscodes.html">here</a> which lists detailed descriptions of each.</p>
89+
<p>Suggested usages for the &quot;Top 10&quot; HTTP Response Status Codes are as follows:
90+
<dl>
91+
<dt>200 OK</dt>
92+
<dd>General status code. Most common code used to indicate success.</dd>
93+
<dt>201 CREATED</dt>
94+
<dd>Successful creation occurred (via either POST or PUT). Set the Location header to contain a link to the newly-created resource (on POST). Response body content may or may not be present.</dd>
95+
<dt>204 NO CONTENT</dt>
96+
<dd>Indicates success but nothing is in the response body, often used for DELETE and UPDATE operations.</dd>
97+
<dt>400 BAD REQUEST</dt>
98+
<dd>General error when fulfilling the request would cause an invalid state. Domain validation errors, missing data, etc. are some examples.</dd>
99+
<dt>401 UNAUTHORIZED</dt>
100+
<dd>Error code response for missing or invalid authentication token.</dd>
101+
<dt>403 FORBIDDEN</dt>
102+
<dd>Error code for user not authorized to perform the operation or the resource is unavailable for some reason (e.g. time constraints, etc.).</dd>
103+
<dt>404 NOT FOUND</dt>
104+
<dd>Used when the requested resource is not found, whether it doesn't exist or if there was a 401 or 403 that, for security reasons, the service wants to mask.</dd>
105+
<dt>405 METHOD NOT ALLOWED</dt>
106+
<dd>Used to indicate that the requested URL exists, but the requested HTTP method is not applicable. For example, POST <em>/users/12345</em> where the API doesn't support creation of resources this way (with a provided ID). The Allow HTTP header must be set when returning a 405 to indicate the HTTP methods that are supported. In the previous case, the header would look like "Allow: GET, PUT, DELETE"</dd>
107+
<dt>409 CONFLICT</dt>
108+
<dd>Whenever a resource conflict would be caused by fulfilling the request. Duplicate entries, such as trying to create two customers with the same information, and deleting root objects when cascade-delete is not supported are a couple of examples.</dd>
109+
<dt>500 INTERNAL SERVER ERROR</dt>
110+
<dd>Never return this intentionally. The general catch-all error when the server-side throws an exception. Use this only for errors that the consumer cannot address from their end.</dd>
111+
</dl>
112+
</p>
113+
<h2>Offer Both JSON and XML</h2>
114+
<p>Favor JSON support unless you're in a highly-standardize and regulated industry that requires XML, Schema validation and namespaces. And unless the costs of offering both JSON and XML are staggering, however, offer them both. Ideally, let consumers switch between using the HTTP Accept header, or by just changing an extension from .xml to .json on the URL.</p>
115+
<p>Be warned though, as soon as we start talking about XML support, we start talking about schemas for validation, namespaces, etc. Unless required by your industry, avoid supporting all that complexity initially, if ever. JSON is designed to be simple, terse and functional. Create your XML to look like that if you can.</p>
116+
<p>In other words, make the XML that is returned more JSON-like: simple and easy to read, without the schema and namespace details present&mdash;just data and links. If it ends up being more complex than this, the cost of XML will be staggering. In my experience nobody uses the XML responses anyway over the last several years. It's just too expensive to consume.</p>
117+
<p>Note that <a href="http://json-schema.org/">JSON-Schema</a> offers schema-style validation capabilities, if you need that sort of thing.</p>
66118
<h2>Create Fine-Grained Resources</h2>
67-
<p>When starting out, it's much easier to create APIs that mimic the underlying application domain or database architecture of your system. Eventually, you'll want aggregate servicesservices that utilize multiple underlying resources to reduce chattiness. But it's much easier to create larger resources later from individual resources than it is to create fine-grained or individual resources from larger aggregates. Make it easy on yourself and start with small, easily defined resources, providing CRUD functionality on those. You can create those use-case-oriented, chattiness-reducing resources later.</p>
119+
<p>When starting out, it's much easier to create APIs that mimic the underlying application domain or database architecture of your system. Eventually, you'll want aggregate services&mdash;services that utilize multiple underlying resources to reduce chattiness. But it's much easier to create larger resources later from individual resources than it is to create fine-grained or individual resources from larger aggregates. Make it easy on yourself and start with small, easily defined resources, providing CRUD functionality on those. You can create those use-case-oriented, chattiness-reducing resources later.</p>
68120
<h2>Consider Connectedness</h2>
69-
<p>One of the principles of REST is connectedness—via hypermedia links. While services are still useful without them, APIs become more self-descriptive when links are returned in the response. At the very least, a 'self' reference informs clients how the data was or can be retrieved. Additionally, utilize the Location header to contain a link on resource creation via POST. For collections returned in a response that support pagination, 'first', 'last', 'next' and 'prev' links at a minimum are very helpful.</p>
121+
<p>One of the principles of REST is connectedness—via hypermedia links (search HATEOAS). While services are still useful without them, APIs become more self-descriptive and discoverable when links are returned in the response. At the very least, a 'self' link reference informs clients how the data was or can be retrieved. Additionally, utilize the HTTP Location header to contain a link on resource creation via POST (or PUT). For collections returned in a response that support pagination, 'first', 'last', 'next' and 'prev' links at a minimum are very helpful.</p>
122+
<p>Regarding linking formats, there are many. The HTTP Web Linking Specification (<a href="http://tools.ietf.org/search/rfc5988">RFC5988</a>) explains a link as follows:
123+
<blockquote>a link is a typed connection between two
124+
resources that are identified by Internationalised Resource
125+
Identifiers (IRIs) [<a href="http://tools.ietf.org/search/rfc3987">RFC3987</a>], and is comprised of:
126+
<ul>
127+
<li>A context IRI,</li>
128+
<li>a link relation type</li>
129+
<li>a target IRI, and</li>
130+
<li>optionally, target attributes.</li>
131+
</ul>
132+
A link can be viewed as a statement of the form &quot;{context IRI} has a
133+
{relation type} resource at {target IRI}, which has {target
134+
attributes}.&quot;</blockquote></p>
135+
<p>At the very least, place links in the HTTP Link header as recommended in the specification. Or embrace a JSON representation of this HTTP link style (such as Atom-style links, see: <a href="http://tools.ietf.org/search/rfc4287#section-4.2.7">RFC4287</a>) in your JSON representations. Later, you can layer in more complex linking styles such as <a href="http://stateless.co/hal_specification.html">HAL+JSON</a>, <a href="https://github.com/kevinswiber/siren">Siren</a>, <a href="http://amundsen.com/media-types/collection/">Collection+JSON</a>, and/or <a href="http://json-ld.org/">JSON-LD</a>, etc. as your REST APIs become more mature.</p>
70136
</div>
71137
</div>
72138
<hr>

0 commit comments

Comments
 (0)