If you have never learned about HTTP methods, you are probably confused about what exactly are GET
requests or POST
requests are at this point. When developing web apps, you will encounter several types of HTTP requests:
Let's open up the terminal and navigate ourselves to the Ideator app:
cd ~/desktop/ideator
Let's then type in the following in the terminal and press enter:
rails routes
It should display something like this:
Prefix Verb URI Pattern Controller#Action
root GET / ideas#index
ideas GET /ideas(.:format) ideas#index
POST /ideas(.:format) ideas#create
new_idea GET /ideas/new(.:format) ideas#new
edit_idea GET /ideas/:id/edit(.:format) ideas#edit
idea GET /ideas/:id(.:format) ideas#show
PATCH /ideas/:id(.:format) ideas#update
PUT /ideas/:id(.:format) ideas#update
DELETE /ideas/:id(.:format) ideas#destroy
As you can see, Ruby on Rails applications uses GET
, POST
, PATCH
, PUT
, and DELETE
requests very frequently. Thus, it is important to learn what it actually means.
We will walk you through all of these types of requests so that you can better understand them when they show up.
GET requests are the most commonly uses HTTP request. GET
requests used to retrieve data from a server. For example, if I go to my browser and go to https://techbrain-ideator.herokuapp.com, then I am making a GET
request to the server to retrieve the data of the web page. The browser then receives the information about the web page and displays it for us.
In short, whenever we access a web page, we are making a GET
request.
A POST request is used to send data to the server, for example, customer information, file uploads, etc. using HTML forms. It is often used to create new objects. For example, in index.html.erb
in our Ideator app, we have the following form:
<%= simple_form_for Idea.new do |f| %>
<div class="flex items-center justify-between p-4 md:p-5 border-b rounded-t dark:border-gray-600">
<h3 class="text-xl font-semibold text-gray-900 dark:text-white">Add New Idea</h3>
<button type="button" class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" data-modal-hide="default-modal">
<svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/>
</svg>
<span class="sr-only">Close modal</span>
</button>
</div>
<div class="p-4 md:p-5 space-y-4">
<%= f.input :description, input_html: { rows: 5, class: 'text-gray-800' } %>
<%= f.input :author, input_html: { class: 'text-gray-800' } %>
</div>
<div class="flex items-center p-4 md:p-5 border-t border-gray-200 rounded-b dark:border-gray-600">
<%= f.submit 'Submit', class: 'text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800' %>
<button data-modal-hide="default-modal" type="button" class="text-white bg-red-700 hover:bg-red-800 focus:ring-4 focus:ring-red-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-red-600 dark:hover:bg-red-700 focus:outline-none dark:focus:ring-red-800">Cancel</button>
</div>
<% end %>
This is a form that allows users to input the information about the new idea and submit it to the server. When the user hits the submit button, a POST
request is made to the Ideator application. In other words, the information that the user filled out in the form is sent to the Ideator server. The application then takes the information and creates a new Idea
based on that information.
In short, POST
requests send data to the server. It is typically used to create new objects; in the case of the Ideator app, POST
requests are made to create a new Idea
object.
DELETE
requests do what you would expect; it is used to delete objects.
For example, in index.html.erb
in our Ideator app, we have the following line of code:
<%= link_to 'Delete', idea_path(idea), method: :delete, class: 'btn btn-sm pull-left', data: {confirm: "Are you sure?"} %>
This piece of code creates a link that deletes an idea from the application. This is an example of a DELETE
request.
PATCH
requests are used to update an object.
For example, in edit.html.reb
in our Ideator app, we wrote the following form:
<%= simple_form_for @idea, method: :patch do |f| %>
<%= f.input :description, input_html: { rows: 5, class: 'text-gray-800' } %>
<%= f.input :author, input_html: { class: 'text-gray-800' } %>
<%= f.submit 'Submit', class: 'text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 me-2 mb-2 dark:bg-blue-600 dark:hover:bg-blue-700 focus:outline-none dark:focus:ring-blue-800 mt-3' %>
<% end %>
We wrote this form so that we can update our ideas. We can edit our ideas using this form. When we submit this form, a PATCH
request is sent to the server. The server then updates the ideas according to the form that we submitted.
Even though PUT
requests and PATCH
requests both map to the update
method (see result of rails routes
), many Ruby on Rails developers don't use PUT
requests anymore, and use PATCH
requests instead.
You don't need to understand exactly why this is, but if you would like to read about the topic, here a few links that explain this well:
In short, don't use PUT
requests, use PATCH
requests instead.
Here is a final overview of all of the HTTP methods in short sentences:
GET
requests are used to retrieve data. When you vist a web page, you are making a GET
request.POST
requests are used to create new objects.DELETE
requests are used to delete objects.PATCH
requests are used to update objects.PUT
requests are not used anymore, use PATCH
requests instead.