Handle POST requests.
(self)
| 129 | ) |
| 130 | |
| 131 | def do_POST(self) -> None: |
| 132 | """Handle POST requests.""" |
| 133 | content_length = int(self.headers.get("Content-Length", 0)) |
| 134 | post_data = self.rfile.read(content_length).decode("utf-8") |
| 135 | |
| 136 | if self.path == "/post": |
| 137 | # Mimic httpbin.org/post - echo POST data |
| 138 | headers: dict[str, str] = {} |
| 139 | for header_name, header_value in self.headers.items(): |
| 140 | headers[header_name] = header_value |
| 141 | |
| 142 | json_data: Any = None |
| 143 | try: |
| 144 | if post_data: |
| 145 | json_data = json.loads(post_data) |
| 146 | else: |
| 147 | json_data = {} |
| 148 | except json.JSONDecodeError: |
| 149 | json_data = None |
| 150 | |
| 151 | data: dict[str, Any] = { |
| 152 | "args": {}, |
| 153 | "data": post_data, |
| 154 | "json": json_data, |
| 155 | "headers": headers, |
| 156 | "origin": self.client_address[0], |
| 157 | "url": f"http://{self.headers.get('Host', 'localhost')}{self.path}", |
| 158 | } |
| 159 | console.print("[green]→ POST /post - Returning echo data[/green]") |
| 160 | self.send_json_response(data) |
| 161 | else: |
| 162 | console.print(f"[red]→ POST {self.path} - Not Found[/red]") |
| 163 | self.send_json_response( |
| 164 | {"error": "Not Found", "path": self.path}, status_code=404 |
| 165 | ) |
| 166 | |
| 167 | |
| 168 | def main() -> int: |