Skip to content

From TCP to HTTP to REST

How application communication evolves from raw sockets to standardized APIs.

This article assumes a basic understanding of TCP socket programming.


1. Understanding the Protocol Layers

For two applications running on different devices to communicate, we need more than just a way to reach the destination device. We also need a way to transport data and define what that data actually means.

This is where the different network protocol layers come into play.

1. IP: Where Should the Data Go?

The IP layer is responsible for addressing and routing packets to their destination.

It helps identify the source and destination devices using IP addresses and determines how packets travel across networks.

However, IP does not define what the application data means or how an application should communicate with another application.

2. TCP: How Should the Data Be Transported?

TCP operates on top of IP and provides a reliable, ordered byte stream between applications.

It handles transport-related responsibilities such as:

  • Establishing and managing connections.
  • Sequencing data.
  • Detecting lost data and retransmitting it.
  • Handling flow control.
  • Delivering bytes in order.
  • Reassembling the received byte stream from TCP segments.

UDP provides an alternative transport mechanism with a different set of trade-offs.

The important distinction is that TCP and UDP deal with how data is transported, not what the application data represents.

They do not understand whether the data contains an email, a file, a web request, or a temperature reading.

3. Application Layer: What Exactly Should We Send?

Now consider building different applications.

An email application needs to exchange information such as:

  • Sender
  • Recipient
  • Subject
  • Message body
  • Attachments

An FTP application needs commands and responses for operations such as uploading, downloading, and listing files.

A web application needs a way to request resources, send data, receive responses, and communicate status information.

Each application needs its own rules for structuring and interpreting the data exchanged between the communicating endpoints.

These rules are defined by application-layer protocols.

Examples include:

  • SMTP for email transmission.
  • FTP for file transfer.
  • HTTP for web communication.

The application-layer protocol defines what messages are exchanged and what those messages mean. TCP or UDP handles their transport, depending on the protocol.

Putting the Layers Together

The responsibilities can be summarized as:

Application Layer
    WHAT to communicate
    Application messages, formats, and semantics
             ↓
Transport Layer (TCP / UDP)
    HOW to transport the data
    Connections, sequencing, reliability, etc.
             ↓
Network Layer (IP)
    WHERE to deliver the packets
    Addressing and routing

Each layer solves a different problem.

IP gets packets toward the destination. TCP or UDP provides transport. The application-layer protocol defines what the communicating applications exchange.


2. A Practical Example: HTTP

Let's take HTTP as an example of an application-layer protocol.

HTTP defines how a client and server exchange requests and responses.

For example, HTTP defines standard methods such as:

Method Purpose
GET Retrieve a resource
POST Submit data
PUT Create or replace a resource
PATCH Partially update a resource
DELETE Delete a resource

But how does this communication actually happen?

Let's follow a simplified example of a browser requesting a web page.

Communication Sequence

1. The server starts listening

The web server creates a TCP socket, binds it to a port (for example, port 80), and starts listening for incoming connections.

2. The client establishes a TCP connection

The browser creates a TCP connection to the server's IP address and port 80.

3. The client sends an HTTP request

Once the TCP connection is established, the browser sends an HTTP request:

GET /index.html HTTP/1.1
Host: example.com
Accept: text/html

4. The server sends an HTTP response

The server receives the request, processes it, and sends an HTTP response:

HTTP/1.1 200 OK
Content-Type: text/html

<html>
  <body>Hello World!</body>
</html>

5. The client and server exchange more requests and responses

A web page may require additional resources such as CSS files, JavaScript files, and images.

The browser can send additional HTTP requests and receive corresponding responses over the same TCP connection.

6. The TCP connection is eventually closed

When the connection is no longer needed, it can be closed.

Putting It Together

Browser (Client)                 Web Server
       |                              |
       |                              | Listen on port 80
       |                              |
       |------ TCP Connection ------->|
       |                              |
       |------ HTTP GET request ----->|
       |<----- HTTP response ---------|
       |                              |
       |------ HTTP GET request ----->|
       |<----- HTTP response ---------|
       |                              |
       |------ TCP Close ------------>|
       |                              |

Notice the separation of responsibilities:

  • TCP establishes and manages the connection and transports the bytes.
  • HTTP defines the request/response messages and their meaning.

This is how an application-layer protocol builds on top of a transport-layer protocol.

One important detail: HTTP/1.1 supports persistent connections. The TCP connection does not have to be closed after every request/response exchange. See Persistent vs Non-Persistent Connections


3. Operation-Oriented vs Resource-Oriented API Design

There are different ways to design an API using HTTP.

In an operation-oriented API, the endpoint represents an operation that the client wants the server to execute. The client passes the required parameters.

For example, to retrieve Apple's stock price:

GET /getStockPrice?symbol=AAPL

Here, getStockPrice represents the operation, and symbol=AAPL is its input parameter.

In a resource-oriented API, the endpoint identifies the resource, and the HTTP method specifies what the client wants to do with it.

For example:

GET /stocks/AAPL

Here, /stocks/AAPL identifies the stock resource, while GET requests its representation.

The fundamental difference is:

  • Operation-oriented: Tell the server what operation to execute.
  • Resource-oriented: Identify the resource and specify the desired action.

Both approaches can use standard HTTP requests and responses. REST follows the resource-oriented approach.


4. REST: Working With Resources

As discussed, REST follows a resource-oriented approach.

Instead of exposing individual operations, an application exposes resources that clients can access and manipulate.

Let's continue with our stock market application.

The API might expose the following resources:

/stocks
/stocks/AAPL
/stocks/AAPL/history
/markets/NASDAQ

Each URL identifies a resource or collection of resources.

REST uses standard HTTP methods to define what the client wants to do with these resources.

For example:

HTTP Method Request Purpose
GET /stocks/AAPL Retrieve stock information
POST /stocks Create a stock resource
PUT /stocks/AAPL Replace a stock resource
PATCH /stocks/AAPL Partially update a stock resource
DELETE /stocks/AAPL Delete a stock resource

The server returns a representation of the requested resource, commonly in JSON format.

For example:

GET /stocks/AAPL HTTP/1.1
Host: example.com

Response:

HTTP/1.1 200 OK
Content-Type: application/json

{
  "symbol": "AAPL",
  "company": "Apple Inc.",
  "price": 250.50,
  "currency": "USD"
}

Notice that REST does not define a new request format or introduce new HTTP methods. It uses the existing HTTP protocol to access and manipulate resources.

Also, the resource does not have to correspond directly to a database record. It represents an entity or concept exposed by the application.

The central idea of REST is to organize an API around resources rather than individual operations, using a uniform interface to interact with them.


5. Standard HTTP Methods Used in REST

HTTP defines several standard methods that specify the intended action of a request.

REST uses these existing HTTP methods to interact with resources.

The commonly used methods correspond to the CRUD operations found in database systems:

CRUD Operation HTTP Method REST Usage
Create POST Create a new resource
Read GET Retrieve a resource
Update PUT Replace an existing resource
Update PATCH Partially modify a resource
Delete DELETE Remove a resource

For example, in our stock market application:

GET /stocks/AAPL
POST /stocks
PUT /stocks/AAPL
PATCH /stocks/AAPL
DELETE /stocks/AAPL

These are all standard HTTP methods. REST does not introduce new methods; it uses the existing HTTP methods as a uniform interface for interacting with resources.


6. Statelessness: HTTP and REST

Before discussing REST, let's understand what stateless and stateful mean in client-server communication.

What Is Stateful Communication?

In a stateful system, the server maintains information about the client's previous interactions.

For example:

  1. A client connects to the server and logs in.
  2. The server maintains the client's session information.
  3. Subsequent requests rely on that session.
  4. The server uses the stored session state to identify the client and maintain its context.

The server remembers information from previous interactions.

What Is Stateless Communication?

In a stateless system, each request contains all the information necessary for the server to process it.

The server does not depend on stored conversational or session context from previous requests.

For example, a client might send:

GET /account/42 HTTP/1.1
Host: example.com
Authorization: Bearer <token>

The request carries the authentication information needed by the server. The server does not need to remember a previous login request to understand the current request.

Is HTTP Stateless?

Yes. HTTP is fundamentally a stateless request/response protocol.

Each HTTP request can be understood and processed independently. HTTP does not require the server to remember previous requests.

However, applications can maintain state using mechanisms such as:

  • Cookies
  • Session IDs
  • Server-side session storage
  • Authentication tokens

For example, a server can issue a session cookie after login and use it to associate subsequent HTTP requests with the same user session.

So, although HTTP itself is stateless, applications built using HTTP can maintain state.

How Does REST Handle State?

REST explicitly requires stateless communication as one of its architectural constraints.

This means every request must contain the information necessary to process it. The server must not depend on stored client session context from previous requests.

For example, a REST API should not require the server to remember that a client previously logged in before it can understand the next request. The client must provide the necessary authentication information with each request, commonly through an authorization token.

There is an important distinction here:

  • REST does not prohibit storing application data on the server. For example, user accounts, stock information, and orders can all be stored in a database.
  • REST prohibits relying on client-specific conversational state stored on the server to interpret subsequent requests.

In short: HTTP is stateless by design. REST makes stateless communication an explicit architectural constraint for the API.


7. Stateless Communication vs Persistent Connections

There are two concepts that are often confused when discussing HTTP and REST:

  • Stateless vs Stateful: Does the server remember client-specific context between requests?
  • Non-Persistent vs Persistent Connection: Is the underlying TCP connection closed after each request, or kept open for reuse?

These are independent concepts.

1. Stateful vs Stateless Communication

In a stateful system, the server maintains client-specific session information between requests.

For example:

Client                  Server
   |                       |
   |------ Login --------->|
   |                       | Store session
   |<----- Session ID -----|
   |                       |
   |------ Request 1 ----->|
   |                       | Use stored session
   |<----- Response -------|
   |                       |
   |------ Request 2 ----->|
   |                       | Use stored session
   |<----- Response -------|

In a stateless system, each request carries the information required by the server to process it.

Client                  Server
   |                       |
   |------ Request 1 ----->|
   |                       | Process independently
   |<----- Response -------|
   |                       |
   |------ Request 2 ----->|
   |                       | Process independently
   |<----- Response -------|

HTTP is fundamentally a stateless request/response protocol. However, applications can maintain sessions using cookies, session IDs, or server-side session storage.

REST explicitly requires stateless communication: the server must not depend on stored client session context to understand a request.

2. Non-Persistent vs Persistent Connections

This is about the lifetime of the underlying TCP connection.

Non-persistent connection:

A new TCP connection is established for each HTTP request/response exchange.

Client                  Server
   |                       |
   |---- TCP Connect ----->|
   |---- HTTP Request ---->|
   |<--- HTTP Response ----|
   |---- TCP Close ------->|
   |                       |
   |---- TCP Connect ----->|
   |---- HTTP Request ---->|
   |<--- HTTP Response ----|
   |---- TCP Close ------->|

Persistent connection:

The TCP connection remains open and can be reused for multiple HTTP request/response exchanges.

Client                  Server
   |                       |
   |---- TCP Connect ----->|
   |---- HTTP Request 1 -->|
   |<--- HTTP Response 1 --|
   |---- HTTP Request 2 -->|
   |<--- HTTP Response 2 --|
   |---- HTTP Request 3 -->|
   |<--- HTTP Response 3 --|
   |                       |
   |---- TCP Close ------->|

HTTP/1.1 uses persistent connections by default. HTTP/2 also uses persistent connections and supports multiple concurrent streams over a single connection.

3. How Do These Concepts Apply to REST?

REST requires stateless communication, but it does not require non-persistent connections.

A REST API can reuse the same TCP connection for multiple requests while processing each request independently.

For example:

One persistent TCP connection
             |
             |---- GET /stocks/AAPL -------->|
             |<--- Stock information --------|
             |                               |
             |---- GET /stocks/MSFT -------->|
             |<--- Stock information --------|
             |                               |
             |---- GET /markets/NASDAQ ------>|
             |<--- Market information -------|

The connection remains open, but each request contains the information needed to process it.

The important distinction:

Concept HTTP / REST
HTTP statelessness Requests are independent at the protocol level
REST stateless constraint Server does not rely on stored client session context
HTTP connection persistence TCP connection can be reused across requests
REST connection persistence Allowed, but not required by REST

Summary: Statelessness describes how requests are handled. Connection persistence describes how the underlying transport connection is reused. A REST API can be stateless while using a persistent TCP connection.


TCP/IP and Network Progamming Page

TCP is NOT Message-Oriented

Persistent vs Non-Persistent Connections