The Request/Response Cycle of the Web

A simple diagram of the request/response cycle

The web is a cycle of requests and responses that flow between clients and servers.

What is a client?
A client is a piece of computer hardware or software that accesses a service made available by a server. Generally, clients are web browsers (like Chrome or Firefox), but clients can also be API’s making requests to another server or the command line (when making cURL requests).

What is an HTTP request?
A HTTP request is a text string generated by the client and sent to the server containing the specifications of the resource the client is asking for. A resource is anything that can accessed via the web. The HTTP request communicates which resource a client wants to interact with and how the client wants to interact with it, along with some metadata held in the header related to the request.

The resource the client wants to interact with is communicated through the URL that is sent with the request. For example, if a user entered http://twitter.com into their web browser, the web browser would send a request to the server looking for the Twitter resource.

What are the different types of request methods that can be included in an HTTP request?

How the user wants to interact with the resource is communicated through the request method. Four of the most common request methods are GET, POST, PUT, and DELTE.

GET requests generally ask for the resource to be returned unchanged. Sending a request to the homepage of Twitter would be a GET request.

POST requests are used to make a submission to the resource. This type of request would typically ask the server to store new information, for example by writing it to a database. Posting a new tweet would be an example of a POST request.

PUT requests are used to update a resource. Editing a tweet would be an example of a PUT request.

DELETE requests are used to remove a resource. For example, deleting a tweet would be an example of a DELETE request.

What is an HTTP response?
An HTTP response is what is sent by a server to a client in response to an HTTP request. These responses contain a status code and if the request was successful, the requested resource. An example status code for a successful request would be “200” and an example status code for an unsuccessful request would be “404”. Other common status codes include “300” for redirects and “500” for server errors.

Github’s 404 page when making an invalid request

HTTP responses also include a message body. If the client is a browser, the HTML body is generally an HTML page that the browser can then render. If the client is an API or Command Line, the message body could be a JSON or XML steam. An HTTP response would also contain a header with metadata related to the response, similar to an HTTP request header.

How does the server know what to respond with?
All resources are hosted on a server. The server’s location on the web can be identified by its IP address, however, IP addresses aren’t particularly user friendly, and instead we use URLS (such as http://www.google.co.uk) to search for a resource.

Once the client’s request has reached the server, the server will search for and return the information the client is requesting. Often times, this means querying a database, loading the information into an html page, and returning the HTML text to the user in the body of the HTTP response.

What happens once the client receives the HTTP response back from the server?
Once the client receives the HTTP response, the browser will render the HTTP response body message. This message is either the requested resource (for example, a web page) or display a message relating to status code if the request wasn’t successful.

Thanks for reading to the end, please give me a few claps if you found this post helpful!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store