The Request - Response Cycle
Published: 2021-03-08 18:38:57

So we've learned Ruby, and started thinking about building our own website. Most people, like myself, would want to just jump right in with Rails, HTML, and making everything look pretty with CSS.

But first, we have to understand what's happening when it comes to browsing websites before we can code our own.

So just how does the web work? There are many components to the web, like the internet itself, hardware we use to connect and browse, and they're all connected with HTTP (Hypertext Transfer Protocol). 

__What is HTTP?__

HTTP is a protocol that is used to transfer data over the web. HTTP [uses a set of methods that perform various tasks.](https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods) 

To understand what happens when we browse the web, we need to know about the client and server interactions, and the "request-response" cycle.

__What is a client?__

A client is us! Or more specifically, our computer, phone, or any other hardware that has a browser to access the internet (like Safari, Chrome, or Firefox). We type in a website, and get a response from a server.

__What is a server?__
A server is a program or device that responds to client requests. It returns information to the browser so we can get the information we requested. Servers can access databases to retrieve and store information as needed.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0uds20qr7ohdin8k2z5j.png)

If we have our browser open, we'll go to www.https://flatironschool.com, we'll be taken to the Flatiron School main page. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q32jy1rc4lpdyliq5841.png)


Our browser (the client) submits a HTTP request to the server (where Flatiron's website is hosted). The server returns a response, and we get back a HTML page to our browser. 

So to simplify;

The _client_ sends an HTTP request on the web.
The _server_ receives the request, and returns an HTTP response to the browser.

Another example would be checking your Facebook feed. When you access Facebook in the browser, you initiate a request to Facebook's server, and they respond with a display of the login page. You go ahead and log in (another request-response, as you're requesting to log in and the server accesses it database and says, "Hey, it's you, you're good!")

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vlq1gh6vzgjjsz6xix49.png)

Then you're presented with your Facebook feed. All your family and friends selfies, status updates, and location tags are all products of Facebook getting a request to show your feed, then returning a bunch of information stored in its database. All of that information is displayed neatly in your browser.

Every step of loading Facebook.com, logging in, and viewing your feed is an example of the request-response cycle. Note that there can be multiple request and responses happening at the same time! That's how we get a bunch of information so quickly instead of having to wait for each bit to load one by one.

The client side handles all the user interface, and the server stores and retrieves data, processes any logic built into the website, and many other things.

To dive even further concerning the Client-Server, Mozilla's documentation [can be found here.](https://developer.mozilla.org/en-US/docs/Learn/Server-side/First_steps/Client-Server_overview) 

To learn more about HTTP, see [their HTTP documentation.](https://developer.mozilla.org/en-US/docs/Web/HTTP/Overview)