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.
- It starts with a website URL, Uniform Resource Locator, being entered in a web browser from a client (your computer).
- 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.
- The web server receives the request and looks for the HTML webpage (using the information provided in the URL).
- If the page exists, the web server prepares the response and will send it back to your browser.
- Your web browser receives the response and loads the HTML page.
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.
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.