HTTP - The Backbone of API Communication

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.

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": "Alice"
    }
    
  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

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 Versions in Brief

VersionKey Characteristics
HTTP/0.9Single-line protocol with only GET method and HTML-only responses
HTTP/1.0
  • Added HEAD/POST methods, headers
  • Multiple content types
HTTP/1.1
  • Introduced persistent connections
  • Request pipelining (sending multiple-inflight requests before the arrival of the responses of earlier request)
  • Mandatory Host header (helped in virtual hosting)
HTTP/2.0
  • Prioritized resposes / multiplexing (If 2 images requested, sends small image before bigger image)
  • Server push (send resources to the client without it requesting). Server push is deprecated by most browsers including Chrome in 2022, Firefox in 2024
  • Binary protocol with header compression
HTTP/3.0
  • QUIC-based (UDP) transport with built-in TLS 1.3
  • Resolves Head-on-line blocking

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