DEV Community

Cover image for Learning REST API by Building a Tiny HTTP Server in Python
Yogender
Yogender

Posted on

Learning REST API by Building a Tiny HTTP Server in Python

 I recently started learning backend development more deeply, especially REST APIs, HTTP, and how clients and servers communicate.

Instead of only reading theory, I wanted to actually see what happens when a request is sent to a server.

So I created a small HTTP server in Python using BaseHTTPRequestHandler and tested it using curl.

What I Built

I made a simple server running on:

localhost:8000
Enter fullscreen mode Exit fullscreen mode

When I send a request to:

curl -v http://localhost:8000/
Enter fullscreen mode Exit fullscreen mode

the server returns a JSON response like this:

{
  "message": "Hello bro, this is your HTTP response"
}
Enter fullscreen mode Exit fullscreen mode

What I Observed

Using verbose mode in curl, I could clearly see the response details:

HTTP/1.0 200 OK
Content-Length: 52
Content-Type: application/json
Server: BaseHTTP/0.6 Python/3.10.0
Enter fullscreen mode Exit fullscreen mode

This helped me understand that an HTTP response is not just data. It has multiple parts:

  1. Status code
  2. Headers
  3. Body

The server sends the status code first, then headers like Content-Type and Content-Length, and finally the actual JSON body.

What I Learned

This small experiment helped me understand some important backend concepts:

  • How a client sends a request
  • How the server receives that request
  • How routes are handled
  • Why status codes matter
  • Why headers like Content-Type are important
  • How JSON data is sent back to the client
  • How tools like curl help debug APIs

Before doing this, REST APIs felt like a high-level concept. But after seeing the raw request and response, things started becoming much clearer.

Next Step

Now I want to move from a basic Python HTTP server to FastAPI and understand how modern backend frameworks make API development easier.

I also want to explore:

  • Request methods like GET, POST, PUT, and DELETE
  • Request body handling
  • Query parameters
  • Path parameters
  • Status codes
  • Middleware
  • Authentication
  • Database connection

Final Thought

Sometimes the best way to learn backend development is not by starting with a big framework directly, but by first understanding what is happening underneath.

Building this tiny server helped me see the foundation of REST APIs more clearly.

I am still learning, but experiments like this are making the concepts much easier to understand.

Have you ever tried building a basic HTTP server before using a framework?

Top comments (0)