HTTP and Network Request Fundamentals
🎯 Introduction
This article explains how frontends and backends communicate over HTTP, how URLs are structured, how HTTP methods differ, and what requests and responses contain. These fundamentals help you debug network requests, improve page loading performance, and work more efficiently.
🌀 The Client-Server Model
The client and server are two fundamental roles on the internet. The client sends requests, and is typically your browser. The server receives those requests and returns data, such as the backend of a website you visit.
When you enter a URL in your browser's address bar, the browser sends a network request to the server. The server then returns the data needed to display the page. HTTP provides the rules for this exchange.
A network exchange usually has two parts:
- Request: The information a client sends to a server to describe what it wants.
- Response: The data the server returns after receiving a request, such as HTML, images, or JSON.
🔗 URLs and Request Structure
A URL (Uniform Resource Locator) is the address of a resource on the internet. Like your home address, it tells a request where to find something.
A complete URL typically includes the following parts:
- Scheme: Specifies how the browser should access the resource, commonly
http://orhttps://. - Host (a domain name or IP address): Identifies the server, such as
www.example.com. - Port: Defaults to 80 for HTTP and 443 for HTTPS, and can usually be omitted. Think of it as a room number in a hotel.
- Path: Identifies a particular resource on the server, such as
/about. In the hotel analogy, this is an item inside the room. - Query parameters: Provide additional information, usually in the form
?key=value&key2=value2.
GET vs. POST
These are the two most commonly used HTTP request methods.
- GET requests retrieve data from a server, with query parameters appended to the URL. GET requests cannot carry a request body, have limited data capacity, and are unsuitable for sensitive data because parameters appear in the URL.
- POST requests submit data to a server, usually in the request body. They are suitable for sending larger amounts of data or sensitive information.
Common HTTP Methods
Besides GET and POST, other commonly used methods include:
- PUT: Updates a resource on the server.
- DELETE: Deletes a resource from the server.
🧩 Request and Response Structure
Requests and responses follow a defined structure so that clients and servers can interpret each other's messages.
Request Structure
- Headers: Carry request metadata, such as browser type, language, and authorization information.
- Body: Contains data sent to the server, such as form values, typically in POST or PUT requests.
Response Structure
- Status code: Indicates the outcome of processing the request.
- Headers: Provide additional information, such as the content type and caching policy.
- Body: Contains the actual data returned to the client, such as page content, images, or JSON.
Common Status Codes
- 200 OK: The request succeeded and the server returned the data successfully.
- 301 Moved Permanently: The resource has permanently moved, and the browser redirects to its new address.
- 403 Forbidden: The server refuses access because the client lacks permission.
- 404 Not Found: The requested resource does not exist.
- 500 Internal Server Error: An internal server error occurred.
❓ Questions and Answers
Q1: What is the difference between HTTP request headers and the request body?
Headers contain metadata, such as browser type and language. The body contains the actual data being sent and is typically used in POST or PUT requests.
Q2: Why are GET requests unsuitable for sensitive information?
GET request parameters appear in the URL, where they can be intercepted or logged, making them unsuitable for sensitive information.
Q3: How do status codes 404 and 500 differ?
404 means the resource requested by the client does not exist and is a client error. 500 indicates an internal server error.
🧪 Exercises
Exercise 1: Inspect Browser Network Requests
Open a website you like and find the Network panel in your browser's developer tools. Reload the page, select a request, and inspect its URL, method, and status code. Write down what each one means.
<!DOCTYPE html>
<html>
<head>
<title>Network Request Inspection Exercise</title>
</head>
<body>
<h1>Open developer tools to inspect network requests</h1>
<p>Reload the page, open the Network panel, and select a request to view its details.</p>
</body>
</html>
Exercise 2 - Simulate a GET Request
Use your browser's address bar to send a GET request with query parameters, for example:
https://httpbin.org/get?name=beginner&level=introductory
Inspect the response to understand how query parameters are passed in a URL.
🎉 You now understand HTTP fundamentals, URL structure, and the essentials of requests and responses! Keep practicing to strengthen your frontend skills!
