HTTP - The Backbone of API Communication

02 Mins

Hypertext Transfer Protocol is used to transfer Hypertext (outdated term for web related text) and is the most common protocol for web and API communication. HTTP is:

  • Stateless: Each request is independent; the server doesn’t remember what you did before.
  • Request-response based: The client sends a request, the server sends back a response.

Stateless but Extendable

Although HTTP is stateless, meaning the server doesn’t remember previous requests, we use tools like Cookies, Sessions, Tokens (JWTs) to maintain context across multiple interactions.


HTTP Request Flow (How It Works)

  1. Your browser or app opens a TCP connection to the server.

  2. It sends an HTTP request:

    GET /user/42 HTTP/1.1
    Host: api.example.com
    
  3. The server responds:

    200 OK
    Content-Type: application/json
    
    {
      "id": 42,
      "name": "Arjit"
    }
    
  4. Connection is either closed or reused, depending on HTTP version.


Common HTTP Methods

MethodUse
GETFetch data (e.g., get user profile)
POSTCreate a resource (e.g., add new user)
PUTReplace a resource (e.g., update user info)
PATCHModify part of a resource
DELETERemove a resource
OPTIONSDiscover supported methods for a resource
TRACEDiagnostic method for debugging
CONNECTEstablish a tunnel (e.g., HTTPS)

HTTP Headers

HTTP headers are name-value pairs that carry metadata with requests and responses.

Examples:

  • Content-Type: application/json - tells the server what kind of data you’re sending
  • Authorization: Bearer <token> - used for authentication
  • Cookie: session_id=xyz - Maintains session info
  • User-Agent: Chrome/120.0 - info about the client

HTTP Versions in Brief

VersionKey Characteristics
HTTP/0.9
  • Single-line protocol with only GET method and HTML-only responses. Responses are strictly raw HTML without headers or status codes.
HTTP/1.0
  • Added HEAD/POST methods, headers
  • Multiple content types using MIME types
  • Opened a new TCP connection for every single request-response pair.
HTTP/1.1
  • Introduced persistent connections (keep alive)
  • Request pipelining (sending multiple-inflight requests before the arrival of the responses of earlier request)
  • Mandatory Host header (helped in virtual hosting on a single IP)
  • Introduced chunked transfer encoding for data streaming.
HTTP/2.0
  • Prioritized resposes (If 2 images requested, sends small image before bigger image).
  • Introduced multiplexing (splitting data into frames to interleave multiple requests/responses concurrently over a single connection)
  • Server push (send resources to the client without it requesting). Server push is laregely deprecated.
  • Replaced text with a binary protocol and introduced HPACK header compression
HTTP/3.0
  • Switched from TCP to the QUIC transport protocol running over UDP with built-in TLS 1.3
  • Solved Transport-Layer HoL blocking; a dropped packet on one stream no longer stalls the rest of the connection.

Note - Most HTTP connection should use HTTPS over HTTP/2 and if that doesn’t work should use HTTP 1.1