What is HTTP?
HTTP, short for Hypertext Transfer Protocol, is the rules and instructions clients and servers follow to communicate and exchange files over the internet. You can think of HTTP as the language clients and servers use to communicate over the web.
The internet, as we know it, is a group of clients and servers communicating with one another.
- A client is the software a user uses to connect to a server. For example, web browsers.
- A server is a system with database containing the resources that the client wants to access. For example, web servers.
When a browser or web app needs to access a webpage, it sends an HTTP request to the web server. The server then returns with an HTTP response containing an HTTP status code and the requested resource if available at that location.
HTTP is different from HTTPS, which means Hypertext Transfer Protocol Secure. While HTTP and HTTPS use the same protocols, HTTPS is secure, as it encrypts the data sent between the browser and server to prevent it from being accessed by third parties who may use it for malicious purposes.
How HTTP Requests Work
When a client, like a web browser, wants to access some resource on a server, it sends an HTTP request to the server, requesting access to the resource. The specific details of the HTTP request will vary, but it typically consists of several components, including:
- The HTTP method
- The HTTP version
- The Host header
For example, this is an HTTP request containing the HTTP version (HTTP/1.1
), HTTP method (GET
), and Host header specifying the URL of the resource being requested (tuodominio.com
).
GET /index.html HTTP/1.1
Host: yourdomain.com
The HTTP request may contain additional information, including the HTTP headers and body. For example, the code below contains the HTTP headers including:
- User-agent specifying the details of the browser
- Accept header specifying the media types acceptable for the response
- Accept-Encoding header indicating the content-encoding types the client understands
- Accept-Language header specifying the preferred languages for the response
- Connection header requesting that the network connection remains open after the current communication completes
GET /index.html HTTP/1.1
Host: yourdomain.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.5
Connection: keep-alive
The server processes the HTTP request and sends an HTTP response to the browser. This response contains the requested resource (if it is available at the URL) and an appropriate status message indicating the outcome of the request.
The HTTP response may look like the one shown below:
HTTP/1.1 200 OK Content-Type: text/html; charset=UTF-8 Content-Length: 456 Connection: keep-alive <!DOCTYPE html> <html> <head> <title>My Homepage</title> </head> <body> <h1>Welcome to My Homepage</h1> <p>This is the content on my homepage.</p> </body> </html>
The above code contains the HTTP version (HTTP/1.1
) and HTTP status code (200 ok
). It also contains the header and body.
The body contains the HTML code of the webpage the browser requested. Meanwhile,
The header also contains additional details about the body, including:
- Content-Type heading specifying the media type of the response (
HTML
) and its character encoding (UTF-8
) - Content-Length that specifies the size of the response body in bytes
- Connection header indicating the connection should remain open after the response
Examples of HTTP Response Codes
HTTP status codes are three-digit numbers returned by a server in response to a client’s request, indicating the outcome of that request. These codes are grouped into categories that provide insights into whether the request was successfully processed or if an error occurred.
Some common HTTP status codes include:
- 200 ok
- 301 redirect
- 302 redirect
- 304 not modified
- 307 temporary redirect
- 403 forbidden
- 404 not found
- 410 gone
- 500 internal server error
- 502 bad gateway
1 200 ok
The 200 (OK) response code indicates that the request was successful and the server has returned the requested resource.
2 Reindirizzamento 301
The 301 redirect status code indicates that the requested resource has been permanently moved to a new URL, and future requests should be directed to the new URL.
3 302 Reindirizzamento
The 302 redirect indicates that the requested resource has been temporarily moved to a different URL. The resource is expected to return to the original location, so future requests should be sent to the original URL.
4 304 Non modificato
The 304 Not Modified status code indicates that the requested resource has not been modified since the client’s last request, so the client should use the one it saved to its cache.
5 307 Reindirizzamento temporaneo
The 307 Temporary Redirect status code indicates that the resource has been temporarily moved to a new location, and the client should use the original request method when sending a request to the new location.
6 403 Proibito
The 403 Forbidden response code indicates that the server understands the client’s request but will not authorize it because the client does not have enough permissions.
7 404 non trovato
The 404 Not Found status code indicates that the server cannot find the resource at the requested location. In other words, the resource does not exist on the server.
8 410 Andato
The 410 Gone status code signifies that the requested resource is unavailable on the server. It has been permanently removed and is not coming back.
9 500 Errore interno del server
The 500 Internal Server Error response code indicates that the server encountered an unexpected server condition that prevented it from fulfilling the request.
10 errore di connessione 502 Bad Gateway
The 502 Bad Gateway status code indicates that the server, while acting as a proxy or gateway server, received an invalid response from another server while trying to request the resource.