Created
January 28, 2026 03:36
-
-
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).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python get_body_echo.pySimple JSON
Raw text
Empty body (normal GET)
curl http://localhost:8000/