Setfire Media<\/a>.<\/div>\n<\/div>\nBasically RESTful development is all about nouns, and a complete RESTful design will have the entire functionality of the website broken down into nouns that can be manipulated exclusively by CRUD operations. Each noun will be a controller (and often but by no means always it will also be a database model) and each controller will have up to four functions to represent each CRUD action.<\/p>\n
For example take very simple online CMS with a database of users who can log in and change the text on various pages. A RESTful design might look something like this:<\/p>\n
Noun – ‘user’
\nPOST – Create new user
\nGET – Read user details
\nPUT – Update user
\nDELETE – Delete user<\/p>\n
Noun – ‘page_text’
\nPOST – Create new text
\nGET – Read text
\nPUT – Update text
\nDELETE – Delete text<\/p>\n
As you can see, RESTful design where the nouns are models is very very straightforward and logical. But what about allowing the users to log in? We don’t have a ‘login’ model in the same way as the users and page_texts, but we can represent the action in a very similar way:<\/p>\n
Noun – ‘login’
\nPOST – Log a user in (create a ‘login’)
\nDELETE – Log a user out (delete a ‘login’)<\/p>\n
And if we wanted the ability to preview new text before saving it:<\/p>\n
Noun – ‘preview’
\nPOST – Show (create) a ‘preview’<\/p>\n
Now everyone knows how to send GET and POST requests but what about PUT and DELETE? Despite being part of the HTTP protocol since day one they are surprisingly under-used. All modern server-side languages should accept these as valid HTTP methods but in practice the PUT method is rarely used for an update operation as this can be distinguished from a create operation simply by the presence of a key relating to the noun that will be updated:<\/p>\n
POST request with no key – Create
\nGET request with key – Read
\nPOST data with key – Update
\nDELETE request with key – Delete<\/p>\n
It really is that simple. It doesn’t take much thought to break down any operation into one or more nouns with one or more of the CRUD operations being performed on each. Do that and you’ll be programming RESTfully in no time.<\/p>\n","protected":false},"excerpt":{"rendered":"
REST or Representational State Transfer development is web development with controller functions geared around the four HTTP request types (or verbs) – POST, GET, PUT and DELETE, equating these (classically but by no means exclusively) to the CRUD operations Create, Read, Update and Delete respctively. Once you start developing in REST the advantages of this […]<\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[31],"tags":[44,36,34,35],"class_list":["post-137","post","type-post","status-publish","format-standard","hentry","category-programming-techniques","tag-development","tag-http","tag-programming","tag-rest"],"_links":{"self":[{"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/posts\/137","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/comments?post=137"}],"version-history":[{"count":6,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions"}],"predecessor-version":[{"id":201,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/posts\/137\/revisions\/201"}],"wp:attachment":[{"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/media?parent=137"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/categories?post=137"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.setfiremedia.com\/blog\/wp-json\/wp\/v2\/tags?post=137"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}