If you’ve used or worked on the internet, you’ve used the Hyper Text Transfer Protocol or HTTP. It’s the foundation of how everything on the internet communicates. It works using a request-response protocol. The client requests files from the server, and the server responds. To get a better understanding of what HTTP is, let’s break it down word for word.
Hypertext – Structured text that uses logical links (Hyperlinks) between nodes containing text.
Transfer – Move from one place to another.
Protocol – A set of rules governing the exchange or transmission of data between devices.
Let’s put all of this together. HTTP is Structured text that uses logical links between nodes and transfers from one place to another using a set of rules. That makes sense, right?
How HTTP Works
When you type a URL (Uniform Resource Locater) into your address bar and press enter you’re sending a request to a server at that address. You’re basically saying “Hey server, please send me the data from this URL”. This is what’s known as an HTTP Request. Requests are sent by the client which in this case is the browser. When the server receives your request, it sends an HTTP Response. The response carries with it:
- A status line
- Zero or more header lines followed by CRLF
- An empty line, indicating the end of the header fields
- Optionally a message body
1.) Protocol: The first part of the URL specifies the protocol to use to communicate with the server. Generally you’ll see HTTP or HTTPS (Hypertext Transfer Protocol Secure).
2.) Domain Name: This indicates what Web Server is being requested. You can also specify the IP Address of the Web Server, but this is used less often due to convenience.
3.) Port: By default HTTP uses port 80 to serve HTML files so this usually isn’t specified in a URL. The exception to this is if you’re connecting to an Application Server that can host multiple applications on separate ports.
4.) Path to the file: The path to the requested resource on the Web Server.
5.) Parameters: These are the extra parameters passed to the server. They’re a list of key/value pairs separated by an ampersand (&).
A Request Verb is applied to a URL & tells the server whether the client is requesting to GET data, POST data, PUT data, or DELETE data. This is a very important piece in HTTP communications, because the server needs to know what the client request is asking. There’s a few other request verbs that exist, HEAD, TRACE & OPTIONS, but I won’t talk about them in this article as they generally aren’t used.
- GET – Fetches an existing resource on the server.
- POST – Creates a new resource on the server. These request usually carry a payload that specifies what new resource should be created.
- PUT – Updates an existing resource on the server.
- DELETE – Deletes an existing resource on the server.
HTTP Response Status Codes
We’ve all experienced a 404 error. This is the status code that’s sent as part of an HTTP response when the resource requested doesn’t exist. The status code tells the client how to interpret an HTTP responses. It’s a very important part of an HTTP response because the client needs to know what the response is saying.
1xx: Informational Messages – Tell the client that the request was received and understood, but that the client should wait for a final response.
2xx: Successful – Tell the client that the request was received, understood and accepted.
- 200 OK.
- 202 Accepted: The request has been accepted for processing, but the processing has not been completed.
- 204 No Content: The server successfully processed the request and is not returning any content.
- 205 Reset content: The server successfully processed the request, but is not returning any content. Unlike a 204 response, this response requires that the requester reset the document view.
- 206 Partial Content: The server successfully processed the request and is not returning any content.
3xx: Redirection – Tell the client that another step is needed. The most common step is to go to a new URL to fetch the resource.
- 301 Moved Permanently: This and all future request should be directed to the given URL.
- 303 See Other: Tell the client to look at another URL.
- 304 Not Modified: The response to the request can be found under another URL using the GET method.
4xx: Client Error – Tell the client that an error seems to be coming from the client.
- 400 Bad Request: The server cannot process the request due to a client error (e.g., malformed request syntax, size too large, invalid request message framing, or deceptive request routing).
- 401 Unauthorized: Similar to 403 Forbidden. Tells the client that authentication is required, has failed, or hasn’t been provided yet.
- 403 Forbidden: The request is valid, but the server is refusing action.
- 405 Method Not Allowed: Request method is not supported for the requested resource.
- 409 Conflict: Indicates that the request could not be processed because of the conflict in the request.
5xx: Server Error
- 501 Not Implemented: The server does not recognize the request method.
- 503 Service Unavailable: The server is currently unavailable (It’s overloaded or down for maintenance).
HTTP headers are used by clients and servers to pass additional information to each other. An HTTP header consists of case-sensitive key/value pairs that specify certain things. A lot more information about HTTP headers can be found at developer.mozilla.org.
HTTP Message Body
The HTTP Message Body (if any) is the content that’s sent back to the client from a request.