A Guide to HTTP and the Web
A beginner's guide to HTTP, the protocol that powers the World Wide Web. Learn about the basic request/response model, HTTP methods, and status codes.
Every time you load a web page, watch a video online, or use a web application, you are using HTTP. HTTP, which stands for Hypertext Transfer Protocol, is the foundation of data communication for the World Wide Web. It's the protocol that web browsers and web servers use to communicate with each other.
Understanding the basics of HTTP is a fundamental skill for anyone involved in building for the web.
The Request-Response Model
At its core, HTTP is a simple request-response protocol. The communication always happens in pairs:
- The Request: A client (usually your web browser) sends an HTTP request to a server.
- The Response: The server receives the request, processes it, and sends an HTTP response back to the client.
This cycle happens for every single resource that makes up a web page—the HTML file, each CSS file, each JavaScript file, and each image.
What's in an HTTP Request?
An HTTP request is just a text message with a specific format. It consists of three main parts:
The Request Line: This specifies the HTTP method, the path to the requested resource, and the HTTP version.
GET /index.html HTTP/1.1
Headers: These are key-value pairs that provide additional information about the request, such as the host, the user's browser (
User-Agent
), and the types of content the client can accept (Accept
).Host: www.example.com
User-Agent: Mozilla/5.0 ...
The Body (Optional): For requests that send data to the server (like a
POST
request), the body contains the data, often in a format like JSON.
HTTP Methods (Verbs)
The HTTP method tells the server what action the client wants to perform on the resource. The most common methods are:
GET
: Requests a representation of the specified resource. This is the most common method, used every time you navigate to a web page.POST
: Submits data to be processed to a specified resource. This is often used for submitting web forms or creating a new resource in an API.PUT
: Updates a specified resource with new data.DELETE
: Deletes the specified resource.
These methods are the foundation of RESTful APIs.
What's in an HTTP Response?
After processing the request, the server sends back a response, which also has three main parts:
The Status Line: This includes the HTTP version, a status code, and a status message.
HTTP/1.1 200 OK
Headers: These provide information about the response, such as the content type (
Content-Type
) and the length of the content (Content-Length
).Content-Type: text/html
The Body: This contains the actual resource that was requested, such as the HTML content of a web page or the JSON data from an API.
HTTP Status Codes
The status code is a three-digit number that gives the client a quick way to understand the result of the request. They are grouped into categories:
1xx
(Informational): The request was received, continuing process.2xx
(Success): The request was successfully received, understood, and accepted.200 OK
: The standard response for successful requests.201 Created
: A new resource was successfully created.
3xx
(Redirection): Further action needs to be taken to complete the request.301 Moved Permanently
: The requested resource has been permanently moved to a new URL.
4xx
(Client Error): The request contains bad syntax or cannot be fulfilled.400 Bad Request
: The server could not understand the request.403 Forbidden
: The client does not have permission to access the content.404 Not Found
: The server could not find the requested resource. This is the most famous status code.
5xx
(Server Error): The server failed to fulfill an apparently valid request.500 Internal Server Error
: A generic error message, given when an unexpected condition was encountered on the server.
Conclusion
HTTP is the invisible workhorse of the web. While it has evolved over the years with new versions like HTTP/2, the fundamental request-response model has remained the same. By understanding the basics of requests, responses, methods, and status codes, you gain a foundational understanding of how the entire web works.