What is HTTP?
Tags: http
Published: 2021-05-29 17:04:02


### What is HTTP?

HTTP stands for Hyper Text Transfer Protocol. Lets break it down word by word

- Hyper Text -  They are text which can contain link to other information. For ex: HTML pages
- Transfer - to move from one place to another
- Protocol - set of instructions/rules

So basically HTTP is a set of rules to transfer hyper text between web server and client.

HTTP is an application layer protocol. The application layer is the one with which users can interact. HTTP specs only define the structure of the data to be transferred and how it is actually transferred is taken care of by lower-level protocols.

HTTP uses TCP/UDP to transfer data between server and client. TCP and UDP are transport layer protocols that are responsible for transferring between devices (We will see how data is actually transferred in my next post). HTTP clients and servers communicate using requests and responses.

HTTP is a stateless protocol. It means each request is independent of another request. In other words, if a user makes multiple request servers, then it does not know that there are 4 requests made by the user, at least not according to HTTP protocol. It can be tracked by other means such as sessions, cookies, etc..,

### HTTP Request Structure

A HTTP request consists of 

- Request line
- Headers
- Body (Can be optional)

#### Request Line

Request line consists of HTTP Version and request type.

Common request types are

- GET - Used to get a resource.
- POST - Used to create a resource. Most people will use this type for updating also but the convention is to use POST only for creation.
- PUT - Used to update a resource.
- PATCH - Used to update a resource partially.
- HEAD - Used to get only headers of resources. This can be useful if we want to know the content length or accepted data format without requesting for whole data
- OPTIONS - Used to see which headers are supported.
- DELETE - Used to delete a resource.

#### Headers

These are useful if server/client wants to send additional information.

For ex: User Agent, Time etc.., can be sent in headers

#### Body

These are data to be transferred along with request. GET request do not have body.

Example for request is

```bash
GET / HTTP/2
Host: www.google.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Language: en-US,en;q=0.5
Accept-Encoding: gzip, deflate, br
```

### HTTP Response Structure

A HTTP response consists of

- Status Line
- Headers
- Body

#### Status Line

This contains HTTP status code that indicates the status of the request. You can see the list of status codes [here](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes)

#### Header

This consists of additional information about the response. For ex: Content length, Content type, etc.,

#### Body

This contains the response body. This can be HTML page or XML or JSON etc..,

Example for HTTP response

```bash
HTTP/2 200 OK
date: Sat, 29 May 2021 16:30:20 GMT
content-type: text/html; charset=UTF-8
content-length: 35706

<!doctype html><html>.......</html>
```