TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Designing a RESTful web API

17 pointsby scotchioabout 11 years ago

5 comments

slashdotaccountabout 11 years ago
This article does not describe a RESTful architecture. Several things that do not pass the Litmus test &lt;<a href="http://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven&gt;" rel="nofollow">http:&#x2F;&#x2F;roy.gbiv.com&#x2F;untangled&#x2F;2008&#x2F;rest-apis-must-be-hyperte...</a> or otherwise stick out like a sore thumb:<p>1. Explicit versioning, stuffed into each resource&#x27;s URI<p>This is really bad, it shows the designer has only experience with RPC, but not resource-oriented. Bumping the server version breaks all clients for no good reason. Imagine if the Web sites worked that way: you have to upgrade your browser before you can visit an updated site. The correct way to do it is to version gradually, and express a changed semantic for a resource type by coining a new link relation for it. Do not put semantics in a resource URI as the article advises!<p>2. Utter lack of hypermedia controls<p>The article mentions CSV as possible media type for that entity. As we all know, CSV does not have or support hyperlinks, forms or any other hypermedia control. This is a serious oversight because it&#x27;s not possible to have a RESTful architecture without those controls. Remedy: the designer must implement controls in the message body where natively supported by an entity type (e.g. XLink or XForms for XML) or else fall back to the message header (e.g. Link header&#x2F;URI templates for CSV).<p>3. Fixed URI structuring<p>This is related to point 2 above. Example: &lt;&#x2F;v1&#x2F;stores&#x2F;near?lat=12.34&amp;lon=-12.34&gt; How does the client know how to construct this URI? The article mentions no hyperlinks or other controls. It does say &quot;define the right endpoint names&quot; which I surmise go into some sort of specification document, which is wrong. Doing so makes the server lose control over its URI space and creates unnecessary tight coupling, which is brittle. Express typed resources with link relations, not with the resource URI structure! There must be only exactly one entry point (bookmark) for the client to know, all other URIs are derived by traversing the resources through controls!<p>4. Lack of clue about security<p>The article talks about authentication with OAuth or HTTP basic authentication. OAuth is chiefly for 3rd party authorization, no wonder the article&#x27;s author is confused. Implementing this is workable, but overkill if all you need is authentication, making it much more difficult for a programmer to implement client software correctly, but brings no benefit. HTTP basic authentication is either clear text or susceptible to replay attacks. &lt;<a href="http://www.xml.com/pub/a/2003/12/17/dive.html&gt;" rel="nofollow">http:&#x2F;&#x2F;www.xml.com&#x2F;pub&#x2F;a&#x2F;2003&#x2F;12&#x2F;17&#x2F;dive.html&gt;</a> Remedy: it&#x27;s 2014, just use TLS. Let the transport layer take care of authenticating a user, the application code on the server is free of authentication.<p>5. Reinvents the wheel for error handling<p>Established draft standards exist. Use them. &lt;<a href="http://www.mnot.net/blog/2013/05/15/http_problem&gt;" rel="nofollow">http:&#x2F;&#x2F;www.mnot.net&#x2F;blog&#x2F;2013&#x2F;05&#x2F;15&#x2F;http_problem&gt;</a> &lt;<a href="https://github.com/blongden/vnd.error&gt;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;blongden&#x2F;vnd.error&gt;</a><p>tl;dr Article is pretty much amateur hour, I advise the interested reader to get one of the classic books on REST instead to learn it properly. &lt;<a href="http://oreilly.com/catalog/9780596529260&gt;" rel="nofollow">http:&#x2F;&#x2F;oreilly.com&#x2F;catalog&#x2F;9780596529260&gt;</a> &lt;<a href="http://oreilly.com/catalog/9780596805838&gt;" rel="nofollow">http:&#x2F;&#x2F;oreilly.com&#x2F;catalog&#x2F;9780596805838&gt;</a> &lt;<a href="http://oreilly.com/catalog/9781449358068&gt;" rel="nofollow">http:&#x2F;&#x2F;oreilly.com&#x2F;catalog&#x2F;9781449358068&gt;</a><p>Update: angle brackets are reserved characters for delimiting a URI, not part of a URI. Hackernews, you are out of compliance with RFC 3986, please fix your shit.
arethuzaabout 11 years ago
One the subject of versioning, one thing I rather like is to make the versions resources withing the API itself so you really can stick to the RESTful rule of clients only having to know one URL.<p>e.g. For Salesforce&#x27;s REST API you can see all the versions at:<p><a href="http://na1.salesforce.com/services/data/" rel="nofollow">http:&#x2F;&#x2F;na1.salesforce.com&#x2F;services&#x2F;data&#x2F;</a><p>Documentation:<p><a href="https://www.salesforce.com/us/developer/docs/api_rest/" rel="nofollow">https:&#x2F;&#x2F;www.salesforce.com&#x2F;us&#x2F;developer&#x2F;docs&#x2F;api_rest&#x2F;</a>
评论 #7515662 未加载
jksmithabout 11 years ago
Dig the article. A couple of things. 1) I can see what you mean for handling auth keys in query string as opposed to &quot;key:&quot; using basic auth. I started using the latter pattern after I went through easypost&#x27;s api docs. Even though &quot;key:&quot; is a little unbalanced I guess I still dig the basic auth way. YMMV. 2) On error messages, I wrote some code to provide extended info only if the req host is in a white list (set up your white list in an ini file or whatever). I was originally just throwing back a 500 plus whatever the error returned, but the db error info returned by some of the golang drivers is detailed enough that I didn&#x27;t want to share all of it. Just curious if you had noodled on this issue if it is one for you.
评论 #7516738 未加载
abrichrabout 11 years ago
A couple of notes on Authentication:<p>- Including the clear text API key in the URI makes it very easy for anyone listening in to gain unauthorized access. To get around this, one can either enforce HTTPS, or require a hash of a pass phrase.<p>- Another option to look into is Kerberos.
评论 #7515720 未加载
评论 #7515846 未加载
dickeytkabout 11 years ago
Good article. On the authentication note thought, oath should only be used if you have partners that need to access the API on user&#x27;s behalf. If the API is internal it&#x27;s overkill.<p>I prefer single token methods like jwt.
评论 #7515673 未加载