RESTful verbs

Mahdi Hawary
2 min readSep 29, 2020

--

Over the past several weeks I have been learning to code in rails and to create restful web applications. For the most part I’ve understood just about everything well enough, but for the life of me I couldn’t quite get why we would use one http verb over another to better align with the goal of a RESTful website. So I decided to purposefully create a guide to further my understanding and help others if they were having the same problems.

REST

Rest as well as this cat and you’re golden

So first and foremost what does it mean to be restful. REST is an acronym for REpresentational State Transfer it is an architecture style for designing applications over HTTP. It’s a pattern and not a protocol, implementation is up to the developer.

RESTful verbs

REST verbs specify an action to be performed on a specific resource or resources. There’re tons of http verbs but a few are frequently used, and should be regularly used as a developer. These verbs correspond to CRUD operations, create, read, update, and delete.

GET

get '/resources' do  #your code hereend

The GET method “gets” a representation of a resource from the server. Most web browsing is done using GET requests to the server. If we are being RESTful we use GET only to read data and not change it. They can be called without risking data modification or corruption. GET is also idempotent(no idea how to pronounce so don’t ask), which means no matter how many times it’s called it will always have the same result as a single request.

POST

post '/resources' do  #your code hereend

The POST method is used to create a new resource, or a subordinate resource to some other resource. When you POST to a parent resource the service takes care of associating the new resource with the parent. POST is not idempotent, making two identical POST requests will result in two resources containing the same information.

PUT

POST is very similar to POST but differ in that PUT is idempotent, POST is usually used to update. A create or update request using PUT will always point to the same resource state regardless of the number of requests sent.

put '/resources/:id' do  #your code hereend

PUT is used to update resources.

DELETE

DELETE is pretty self explainable, it deletes a resource, it is also idempotent. Calling DELETE more than once on the same resourceresults in the resource being removed in all cases.

delete '/resources/:id' do  #your code hereend

So to recap in relation to CRUD the methods to use are:

Create: POST

Read: GET

Update: PUT

Delete: DELETE

--

--