Getting to Grips with the HTTP Request — Response Cycle

Himalee
3 min readOct 16, 2018

--

Over the last few weeks I have been working on a Tic Tac Toe (TTT) web application using Sinatra — a Ruby framework that is designed to support the development of web applications. By using Sinatra routes (which include HTTP methods), it enables you to establish the HTTP request-response cycle needed in order to create a web application.

What is HTTP?

Hypertext Transfer Protocol, or HTTP, forms the base for how information is communicated over the internet, which enables us to gain access to websites.

  1. It starts with a website URL, Uniform Resource Locator, being entered in a web browser from a client (your computer).
  2. The browser (Chrome, Safari, Firefox) then sends an HTTP request to a web server which has the ability to store, process and deliver web pages.
  3. The web server receives the request and looks for the HTML webpage (using the information provided in the URL).
  4. If the page exists, the web server prepares the response and will send it back to your browser.
  5. Your web browser receives the response and loads the HTML page.
https://ultimatepeter.com/hacking-resource-the-http-protocol/

HTTP Methods

The two HTTP methods I have used so far are GET and POST. Using Sinatra, the GET and POST requests will be specified at a route, see below, which the web server will pick up and direct it to a specified code block which will run, and return a response to the browser.

http://sinatrarb.com/intro.html

Whilst the POST method is used when additional information is being sent (by the client) to the server to change or update the webpage, the GET method is used to request data from a specified resource — its only responsibility is to retrieve data.

Currently, I have used the GET method in order to display the initial welcome page of the TTT web application and have used the POST method to display a marked board by handling information received on the client side (user can click a button to mark the board).

Sinatra provides the tools in order to create a web application — but understanding how the HTTP request — response cycle works was important in determining what I needed to change from my command line TTT application in order to build a TTT web application, and what can remain the same. I will discuss this further in a Command Line Application vs Web Application blog post.

--

--