What is HTTP?
Hyper Text Transfer Protocol
Developed by Tim Berners-Lee in 1989
HTTP is a protocol that defines how messages are formatted and transmitted, and how web servers and browsers should respond to various commands.
HTTP is a stateless, application-level protocol used for distributed, collaborative, hypermedia information systems.
Key Components of HTTP:
URL (Uniform Resource Locator):
The address used to access a resource on the web.
Example:
https://www.google.com/index.html
Request Methods:
Methods indicating the desired action to be performed on the resource.
Common Methods:
GET: Retrieve data from the server.
POST: Send data to the server.
PUT: Update an existing resource.
DELETE: Remove a resource.
Status Codes:
Codes indicating the result of the HTTP request.
Categories:
1xx (Informational): Request received, continuing process.
2xx (Success): Request successfully received, understood and accepted.
3xx (Redirection): Further action needed to complete the request.
301 Moved Permanently: The requested resource has been permanently moved to a new URL. Browsers and search engines should update their links to the new URL.
302 Found (Previously "Moved Temporarily"): The requested resource has been temporarily moved to a different URL. Browsers may cache the original URL.
303 See Other: The server is directing the client to get the requested resource at a different URL, using a GET request.
307 Temporary Redirect: Similar to 302, indicating that the requested resource has been temporarily moved to a different URL. The client should continue to use the original URL for future requests.
308 Permanent Redirect: The requested resource has been permanently moved to a different URL. The client should update its links to the new URL for future requests.
4xx (Client Error): HTTP status code 400 is known as "Bad Request." This status code indicates that the server cannot process the client's request due to something that is perceived as a client error, such as malformed syntax, invalid request message framing, or deceptive request routing.
5xx (Server Error): HTTP status code 500 is known as "Internal Server Error." This status code indicates that the server encountered an unexpected condition that prevented it from fulfilling the client's request. When a server returns a 500 Internal Server Error status code, it signifies that an error occurred on the server side that was not due to a client error.
Headers:
Key-value pairs sent in both request and response messages to convey additional information. These headers contain additional information about the request or response
Types:
General Headers: Apply to both request and response messages.Examples of general headers include:
Date: Indicates the date and time when the message was sent.
Connection: Specifies whether the connection should be kept alive or closed after the current request/response.
Content-Length: Indicates the length of the message body in octets.
Request Headers: Request headers are sent by the client to the server and provide information about the request being made. Common request headers include:
User-Agent: Identifies the client application making the request (e.g., browser type).
Accept: Specifies the media types that are acceptable for the response.
Authorization: Contains credentials for authenticating the client with the server.
Cookie: Contains stored information from previous interactions with the server.
Response Headers: Response headers are sent by the server to the client and provide information about the response being returned. Common response headers include:
Content-Type: Specifies the media type of the response content (e.g., text/html, application/json).
Location: Used in redirection responses to indicate the new location of a resource.
Cache-Control: Instructs the client on how to cache the response.
Entity Headers: Entity headers are used in both request and response messages and provide information about the content of the message body. Common entity headers include:
Content-Type: Specifies the media type of the message body.
Content-Encoding: Indicates any encoding that has been applied to the message body.
Content-Disposition: Provides information on how the content should be displayed or processed.
Body:
- The actual data being transmitted. Used mainly in POST and PUT requests to send data to the server.
How HTTP Works:
Client Sends a Request:
- A web browser (client) sends an HTTP request to the web server. This request includes a method (e.g., GET, POST), the URL, headers, and optionally, a body.
Server Processes the Request:
- The server processes the request and performs the desired action, such as fetching data or updating a resource.
Server Sends a Response:
- The server sends back an HTTP response, which includes a status code, headers, and optionally, a body containing the requested data or confirmation of the performed action.
Client Receives the Response:
- The client receives the response and renders the data or informs the user about the result of their request.
The Role of HTTP in the Web:
HTTP is the protocol that makes web browsing possible. It allows clients and servers to communicate and exchange data, enabling the web's vast, interconnected network of information. From loading web pages to submitting forms, HTTP underpins almost every interaction on the web.
Common HTTP Methods and Their Uses:
It specify the action to be performed on a resource identified by a URL. Each method serves a distinct purpose and follows a standard convention, which is crucial for the proper functioning of web services and APIs.
- GET: Retrieve data from the server.
Example Use Case: Fetching a web page or retrieving data from a RESTful API.
POST: Submit data to the server to create a new resource.
Example Use Case: Submitting a form or creating a new user in a database.
PUT: Update an existing resource or create a new resource if it does not exist.
Example Use Case: Updating user information or replacing a document in a database.
DELETE: Remove a specified resource from the server.
Example Use Case: Deleting a user account or removing a file.
PATCH: Apply partial modifications to a resource.
Example Use Case:Updating a single field in a user's profile without altering the entire resource.
HEAD: Retrieve the headers of a resource without the body.
Example Use Case: Checking if a resource has been modified or verifying its existence without downloading the entire resource.
OPTIONS: Describe the communication options for the target resource.
Example Use Case: Determining the supported HTTP methods for a given resource.
HTTP Terminology: for Understanding Web Communication:
HTTP: The protocol used for transmitting hypertext requests and information on the World Wide Web.
HTTPS (Hypertext Transfer Protocol Secure): An extension of HTTP that uses encryption (SSL/TLS) to secure data transmission.
URL (Uniform Resource Locator): The address used to access a resource on the web. It includes the protocol (HTTP/HTTPS), domain, path, and optional parameters.
Example: https://www.google.com/index.html
URI (Uniform Resource Identifier): A string that identifies a resource either by location (URL) or by name (URN).
Request: The message sent by a client (e.g., a web browser) to a server to request a resource. It Contains components such as the request method (e.g., GET, POST), URL, headers, and an optional body.
Response: The message sent by a server in reply to a client’s request. It Contains components such as status code, headers, and an optional body.
HTTP Methods (Verbs): Indicate the desired action to be performed on a resource. Common methods include GET, POST, PUT, DELETE, PATCH, HEAD, OPTIONS.
Status Codes: Numerical codes in the HTTP response that indicate the result of the request. (Status code explain above)
Headers: Key-value pairs in both requests and responses that convey additional information about the request or response. (Header types explain above)
Body: The part of an HTTP message where data is transmitted. Used mainly in POST and PUT requests to send data to the server.
Cookies: Small pieces of data stored on the client side and sent with subsequent requests to the same server. Used for session management, personalization, and tracking.
Session: A series of related interactions between a client and server, typically maintained using cookies or session IDs.
Persistent Connection: An HTTP feature that allows multiple requests and responses to be sent over a single TCP connection, reducing latency.
Cache: A mechanism for storing copies of resources to improve performance and reduce the load on the server. Cached resources can be reused to satisfy subsequent requests without contacting the server.
Proxy: An intermediary server that sits between the client and the origin server, forwarding client requests and server responses. Proxies can be used for load balancing, caching, and security.
Load Balancer: A device or software that distributes incoming network traffic across multiple servers to ensure no single server becomes overwhelmed.
SSL/TLS (Secure Sockets Layer/Transport Layer Security): Protocols that provide encryption and security for data transmitted over HTTP, forming the basis of HTTPS.
API (Application Programming Interface): A set of rules and protocols for building and interacting with software applications. HTTP is commonly used as the protocol for web APIs.
REST (Representational State Transfer): An architectural style for designing networked applications, leveraging HTTP methods and URLs to access and manipulate web resources.
WebSocket: A protocol that provides full-duplex communication channels over a single TCP connection, allowing for interactive and real-time web applications.