Table of contents
An application-layer protocol called Hypertext Transfer Protocol (HTTP) is used to send hypermedia documents like HTML. Although it was created for web browser and web server communication, there are other uses for it as well. In the traditional client-server architecture used by HTTP, a client first establishes a connection, sends a request, and then waits for a response. Because HTTP is a stateless protocol, the server does not save any information (state) between requests.
HTTP Example
Request Line
Every HTTP request begins with the request line. This consists of the HTTP method, the requested resource, and the HTTP protocol version.
GET /home.html HTTP/1.1
In this example, GET is the HTTP method, /home.html is the resource requested and HTTP 1.1 is the protocol used.
HTTP Methods
The action that the client wants to do on the web server resource is indicated by the HTTP method.
Common HTTP methods include:
- HTTP Request Headers
The HTTP headers are placed after the request line, then a line break. When an HTTP header is included in the HTTP request, there are several options. A header is a name that is case-insensitive, followed by a:, and then a value.
Typical headers include:
Host: example.com
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:50.0) Gecko/20100101 Firefox/50.0
Accept: */*
Accept-Language: en
Content-type: text/json
The Host header specifies the host of the server and indicates where the resource is requested from.
The User-Agent header informs the web server of the application that is making the request. It often includes the operating system (Windows, Mac, Linux), version and application vendor.
The Accept header informs the web server what type of content the client will accept as the response.
The Accept-Language header indicates the language and optionally the locale that the client prefers.
The Content-type header indicates the type of content being transmitted in the request body.
HTTP Request Body
HTTP requests can optionally include a request body. A request body is often included when using the HTTP POST and PUT methods to transmit data
POST /users HTTP/1.1
Host: example.com
{
"key1":"value1",
"key2":"value2",
"array1":["value3","value4"]
}
HTTP Responses
An HTTP response is delivered once the HTTP request has been processed by the web server.
The status line appears on the first line of the response. This line informs the client whether the request was granted or whether there was a problem.
Http/1.1 200 OK
The HTTP protocol version is the first thing on the line, followed by the status code and a reason phrase. The reason phrase is a textual representation of the status code.
HTTP Status Codes
The first digit of an HTTP status code indicates the category of the response: Information, Successful, Redirection, Client Error, or Server Error.
The common status codes you'll encounter for each category are:
1XX Informational
2XX Successful
3XX Redirection
4XX Client Error
5XX Server Error
- HTTP Response Headers
Optional HTTP response headers are placed after the status line, and then there is a line break. The HTTP response can contain a wide variety of HTTP headers, just like the request headers.
Typical response headers include:
Date: Fri, 11 Feb 2022 15:00:00 GMT+2
Server: Apache/2.2.14 (Linux)
Content-Length: 84
Content-Type: text/html
The Date header specifies the date and time the HTTP response was generated.
The Server header describes the web server software used to generate the response.
The Content-Length header describes the length of the response.
The Content-Type header describes the media type of the resource returned (e.g. HTML document, image, video).
HTTP Response Body
Following the HTTP response headers is the HTTP response body. This is the main content of the HTTP response.This can contain images, video, HTML documents and other media types.
HTTP/1.1 200 OK
Date: Fri, 11 Feb 2022 15:00:00 GMT+2
Server: Apache/2.2.14 (Linux)
Content-Length: 84
Content-Type: text/html
<html>
<head><title>Test</title></head>
<body>Test HTML page.</body>
</html>