SnIPPETsX › def get_headers()
def get_headers()
Python · updated Oct 14, 2026 · 54 lines
main.py
import urllib.request
import json
def get_headers():
headers = {}
print("\nEnter headers (key:value). Press Enter to skip.")
while True:
line = input("Header: ")
if not line:
break
key, value = line.split(":", 1)
headers[key.strip()] = value.strip()
return headers
def main():
print("=== Mini Postman CLI ===")
method = input("Method (GET/POST): ").upper()
url = input("URL: ")
headers = get_headers()
data = None
if method == "POST":
print("\nEnter JSON body (or press Enter to skip):")
body = input()
if body:
data = json.dumps(json.loads(body)).encode("utf-8")
headers["Content-Type"] = "application/json"
try:
req = urllib.request.Request(
url=url,
data=data,
headers=headers,
method=method
)
with urllib.request.urlopen(req) as response:
res = response.read().decode("utf-8")
print("\n--- RESPONSE ---")
print("Status:", response.getcode())
print(res)
except urllib.error.HTTPError as e:
print("\nHTTP Error:", e.code)
print(e.read().decode())
except Exception as e:
print("\nError:", e)
if __name__ == "__main__":
main()
Run this Python snippet
Open this snippet on SnIPPETsX to run the Python code in your browser, edit it together with others in real time, or fork it into your own copy. No install or account is needed.