{"id":43,"date":"2015-05-17T14:00:49","date_gmt":"2015-05-17T18:00:49","guid":{"rendered":"http:\/\/george.hotelling.net\/91percent\/?p=43"},"modified":"2015-05-16T23:05:15","modified_gmt":"2015-05-17T03:05:15","slug":"restful-responses-and-angular","status":"publish","type":"post","link":"https:\/\/george.hotelling.net\/91percent\/2015\/05\/17\/restful-responses-and-angular\/","title":{"rendered":"RESTful responses and Angular"},"content":{"rendered":"<p><a href=\"http:\/\/www.kevinhooke.com\/2015\/04\/23\/testing-for-an-empty-json-document\/\">Testing for an empty JSON document<\/a><\/p>\n<blockquote><p>\n  I have a RESTful service that performs a search and returns the results as JSON, and if there\u2019s no results I return an empty document, {}.\n<\/p><\/blockquote>\n<p>Now I don&#8217;t know what the original author&#8217;s situation is or how much control they have over the API they&#8217;re consuming, but I can definitely project my own experiences on his blog post. At the link above he provides is a good example of the right solution to the wrong problem. It handles the response given, but the response itself is the real problem.<\/p>\n<p>When you are using a RESTful web service to search and it cannot find anything, you shouldn&#8217;t wind up in this situation to start with. The fix isn&#8217;t better client code, the fix is a better web service.<\/p>\n<p>When your service does a search without finding anything, it should return <a href=\"http:\/\/www.bloomberg.com\/404\">404 Not Found<\/a>. I&#8217;ve had to deal with too many web services that only had 2 response codes: 500 if there was an uncaught exception or 200 OK for everything else. <a href=\"http:\/\/www.restpatterns.org\/HTTP_Status_Codes\">There is a sea of other HTTP response codes<\/a> and limiting yourself to just those two limits the usefulness of your service.<\/p>\n<p>If Angular gets a 404 response it won&#8217;t invoke the <code>.success()<\/code> block, it will go straight to <code>.error()<\/code> handler. (<code>.success()<\/code> and <code>.error()<\/code> have their own issues but that&#8217;s another blog post) That would have prevented this situation in the first place. In my project at work we are doing just this: a search that doesn&#8217;t find anything returns a 404. When people have looked at our network traffic they have complained to us about these errors, but by sticking to HTTP semantics we&#8217;ve avoided this all together.<\/p>\n<p>&#8220;But it shouldn&#8217;t return an error code if the service executes without errors!&#8221; &#8211; The 400 series of status codes is for <strong>client<\/strong> errors, requests that can&#8217;t be fulfilled. If I request a URL that doesn&#8217;t have a file on a static website, I get a 404. Why should it be different if I request an ID that doesn&#8217;t exist in your database? Why should a search for something that doesn&#8217;t exist and may never have been OK?<\/p>\n<p>&#8220;But I don&#8217;t want errors in my code!&#8221; &#8211; When you add RESTful web services to your architecture, you are making HTTP part of your execution flow. A 404 response is kind of like an exception: you don&#8217;t want to hide it from your client any more than you would want to <a href=\"http:\/\/www.c2.com\/cgi\/wiki?PokemonExceptionHandling\">catch all exceptions<\/a>. Let the client decide what to do when nothing is found, don&#8217;t try to pretend everything is OK.<\/p>\n<p>So please, if you&#8217;re exposing a web service over HTTP use Status Codes correctly. Or don&#8217;t, it <a href=\"http:\/\/george.hotelling.net\/90percent\/geekery\/http_snob_updated.php\">gives me something to blog about<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Testing for an empty JSON document I have a RESTful service that performs a search and returns the results as JSON, and if there\u2019s no results I return an empty document, {}. Now I don&#8217;t know what the original author&#8217;s situation is or how much control they have over the API they&#8217;re consuming, but I &hellip; <a href=\"https:\/\/george.hotelling.net\/91percent\/2015\/05\/17\/restful-responses-and-angular\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">RESTful responses and Angular<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[6,7],"class_list":["post-43","post","type-post","status-publish","format-standard","hentry","category-programming","tag-angular","tag-rest"],"_links":{"self":[{"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/posts\/43","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/comments?post=43"}],"version-history":[{"count":10,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/posts\/43\/revisions"}],"predecessor-version":[{"id":51,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/posts\/43\/revisions\/51"}],"wp:attachment":[{"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/media?parent=43"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/categories?post=43"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/george.hotelling.net\/91percent\/wp-json\/wp\/v2\/tags?post=43"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}