HTTP

HTTP (Hypertext Transfer Protocol) is the foundation of the World Wide Web. It's a standardized way for clients (like web browsers) to communicate with servers to request and receive data, primarily in the form of web pages.

A HTTP Request

Clients send requests to servers, specifying the resource they want to access (e.g., a webpage, image, or video). Servers respond to requests by sending the requested resource or an error message if the request cannot be fulfilled.

Let's initiate a request using curl:

curl -v https://httpbin.org/get

Use -v for verbose output, we'll see the following output:

> GET /get HTTP/2
> Host: httpbin.org
> User-Agent: curl/8.4.0
> Accept: */*
>
< HTTP/2 200
< date: Mon, 23 Sep 2024 08:27:20 GMT
< content-type: application/json
< content-length: 253
< server: gunicorn/19.9.0
< access-control-allow-origin: *
< access-control-allow-credentials: true
<
{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Host": "httpbin.org",
    "User-Agent": "curl/8.4.0",
    "X-Amzn-Trace-Id": "Root=1-66f32668-47a5d54b0c3385670c2ebbb0"
  },
  "origin": "31.282.0.91",
  "url": "https://httpbin.org/get"
}

The section prefixed with > shows the request headers, while the section prefixed with < displays the response headers. The final part is the response body. Since this is a GET request, there is typically no request body.

The presence of HTTP/2 indicates that this request uses HTTP version 2, which is a binary protocol. The output you see here has been decoded by curl for readability.

HTTP Methods

HTTP defines several methods for different types of requests, such as:

  • GET: Retrieves a resource.
  • POST: Sends data to a server to be processed.
  • PUT: Updates a resource.
  • DELETE: Deletes a resource.
  • PATCH: Partially update a resource.

Status Codes

Servers indicate the outcome of a request using status codes, such as 200 (OK), 404 (Not Found), or 500 (Internal Server Error). Following is a list of frequently used status coeds:

Successful Responses (200-299)

  • 200 (OK)
  • 201 (Created)

Redirects (300-399)

  • 301 (Moved Permanently)

Client Errors (400-499)

  • 400 (Bad Request) incorrect method or content type or invalid payloads
  • 401 (Unauthorized) you need to login
  • 403 (Forbidden) you do have the permission

Server Errors (500 and above)

  • 500 (Internal Server Error) unexpected error
  • 502 (Bad Gateway) invalid response from an upstream server, timeout or crash
  • 503 (Service Unavailable) server is down or unable to handle requests