r/learnpython 2h ago

Building My Own Postman Clone in Python

My first Python project: Postman Clone

I’ve been working on a small project called postman_clone to practice Python and learn more about how APIs and HTTP requests actually work.

I didn’t start with all these features at once. I started with the basics, then kept adding things as I learned. I worked with functions, conditions, user input, JSON, error handling, and HTTP requests, and little by little the project started becoming more complete.

Right now, my project can handle:

  • GET
  • POST
  • PUT
  • DELETE
  • JSON request bodies
  • An optional header
  • An optional query parameter
  • Basic error handling for requests and JSON

There’s still a lot I want to improve. I want to make the code cleaner, add support for multiple headers and query parameters, improve the error handling, and make the whole thing feel more like an actual API testing tool.

I’m still learning, so I’m posting the code here to get some feedback from people who have more experience with Python.

If you see something I could improve, or if there’s something you think I should add next, I’d really appreciate the advice 🙌

This is still a work in progress, but I’m happy with how far I’ve gotten so far.

1 Upvotes

4 comments sorted by

1

u/chinwikhlwi 2h ago
import requests
import json


method = input("Enter method (GET/POST/PUT/DELETE): ").upper()
url = input("Enter URL: ")


# Headers
header_name = input("Enter header name (or press Enter to skip): ")


headers = {}


if header_name:
    header_value = input("Enter header value: ")
    headers[header_name] = header_value


# Query Parameters
param_name = input("Enter parameter name (or press Enter to skip): ")


params = {}


if param_name:
    param_value = input("Enter parameter value: ")
    params[param_name] = param_value


try:


    if method == "GET":
        response = requests.get(
            url,
            
headers
=headers,
            
params
=params
        )


    elif method == "POST":
        body = input("Enter JSON body: ")
        data = json.loads(body)


        response = requests.post(
            url,
            
json
=data,
            
headers
=headers,
            
params
=params
        )


    elif method == "PUT":
        body = input("Enter JSON body: ")
        data = json.loads(body)


        response = requests.put(
            url,
            
json
=data,
            
headers
=headers,
            
params
=params
        )


    elif method == "DELETE":
        response = requests.delete(
            url,
            
headers
=headers,
            
params
=params
        )


    else:
        print("Method not supported!")
        exit()


    print("Status:", response.status_code)
    print(response.text)


except requests.exceptions.RequestException as error:
    print("Request failed:", error)


except json.JSONDecodeError:
    print("Invalid JSON!")import requests
import json


method = input("Enter method (GET/POST/PUT/DELETE): ").upper()
url = input("Enter URL: ")


# Headers
header_name = input("Enter header name (or press Enter to skip): ")


headers = {}


if header_name:
    header_value = input("Enter header value: ")
    headers[header_name] = header_value


# Query Parameters
param_name = input("Enter parameter name (or press Enter to skip): ")


params = {}


if param_name:
    param_value = input("Enter parameter value: ")
    params[param_name] = param_value


try:


    if method == "GET":
        response = requests.get(
            url,
            headers=headers,
            params=params
        )


    elif method == "POST":
        body = input("Enter JSON body: ")
        data = json.loads(body)


        response = requests.post(
            url,
            json=data,
            headers=headers,
            params=params
        )


    elif method == "PUT":
        body = input("Enter JSON body: ")
        data = json.loads(body)


        response = requests.put(
            url,
            json=data,
            headers=headers,
            params=params
        )


    elif method == "DELETE":
        response = requests.delete(
            url,
            headers=headers,
            params=params
        )


    else:
        print("Method not supported!")
        exit()


    print("Status:", response.status_code)
    print(response.text)


except requests.exceptions.RequestException as error:
    print("Request failed:", error)


except json.JSONDecodeError:
    print("Invalid JSON!")


this is the code if you wanna try too

1

u/throwaway980825152 2h ago

you're off to a cracking start, just watch out for how you're building the URL string manually with the query param, it'll bite you when you get to multiple params or ones with special characters

1

u/chinwikhlwi 1h ago

Thanks! Yeah, I see what you mean. I’ll keep that in mind and improve how I build the URL, especially for multiple query params and special characters.