Skip to content

Instantly share code, notes, and snippets.

@valarpirai
Created January 28, 2026 03:36
Show Gist options
  • Select an option

  • Save valarpirai/3c6f0e9188579641bcd06fd40ba29168 to your computer and use it in GitHub Desktop.

Select an option

Save valarpirai/3c6f0e9188579641bcd06fd40ba29168 to your computer and use it in GitHub Desktop.
Here's a simple Python script that creates a tiny HTTP server and echoes back any payload/body sent in a GET request (even though this is very non-standard and most real APIs reject it).
from http.server import BaseHTTPRequestHandler, HTTPServer
import json
class GetBodyEchoHandler(BaseHTTPRequestHandler):
def do_GET(self):
# Read the raw body (if any was sent)
content_length = int(self.headers.get('Content-Length', 0))
body = self.rfile.read(content_length) if content_length > 0 else b''
# Prepare response
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
# Echo back what we received
response = {
"method": self.command,
"path": self.path,
"headers": dict(self.headers),
"body_length": len(body),
"body": body.decode('utf-8', errors='replace') if body else None
}
self.wfile.write(json.dumps(response, indent=2).encode('utf-8'))
# Optional: also handle HEAD just in case
def do_HEAD(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
if __name__ == '__main__':
HOST = 'localhost'
PORT = 8000
server = HTTPServer((HOST, PORT), GetBodyEchoHandler)
print(f"Server running at http://{HOST}:{PORT}")
print("Try: curl -X GET -d '{\"hello\":\"world\"}' http://localhost:8000/test")
print(" or: curl -X GET --data-binary @file.json http://localhost:8000/")
print("Press Ctrl+C to stop\n")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nServer stopped.")
server.server_close()
@valarpirai
Copy link
Author

python get_body_echo.py

Simple JSON

curl -X GET -H "Content-Type: application/json" \
     -d '{"message": "Hello from GET body", "time": "2026"}' \
     http://localhost:8000/echo

Raw text

curl -X GET --data-binary "secret payload here" \
     http://localhost:8000/test/path

Empty body (normal GET)

curl http://localhost:8000/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment