Echo back POST request details similar to httpbin.org/post.
(scope: dict[str, Any], receive: Receive, send: Send)
| 200 | |
| 201 | |
| 202 | async def post_echo(scope: dict[str, Any], receive: Receive, send: Send) -> None: |
| 203 | """Echo back POST request details similar to httpbin.org/post.""" |
| 204 | # Extract basic request info |
| 205 | path = scope.get('path', '') |
| 206 | query_string = scope.get('query_string', b'') |
| 207 | args = get_query_params(query_string) |
| 208 | |
| 209 | # Extract headers and cookies |
| 210 | headers = get_headers_dict(scope) |
| 211 | |
| 212 | # Read the request body |
| 213 | body = b'' |
| 214 | form = {} |
| 215 | json_data = None |
| 216 | more_body = True |
| 217 | |
| 218 | while more_body: |
| 219 | message = await receive() |
| 220 | if message['type'] == 'http.request': |
| 221 | body += message.get('body', b'') |
| 222 | more_body = message.get('more_body', False) |
| 223 | |
| 224 | # Parse body based on content type |
| 225 | content_type = headers.get('content-type', '').lower() |
| 226 | |
| 227 | if body and 'application/json' in content_type: |
| 228 | json_data = json.loads(body.decode()) |
| 229 | |
| 230 | if body and 'application/x-www-form-urlencoded' in content_type: |
| 231 | form_data = parse_qs(body.decode()) |
| 232 | for key, values in form_data.items(): |
| 233 | form[key] = values[0] if len(values) == 1 else values |
| 234 | |
| 235 | body_text = '' if form else body.decode('utf-8', errors='replace') |
| 236 | |
| 237 | # Prepare response |
| 238 | response = { |
| 239 | 'args': args, |
| 240 | 'data': body_text, |
| 241 | 'files': {}, # Not handling multipart file uploads |
| 242 | 'form': form, |
| 243 | 'headers': headers, |
| 244 | 'json': json_data, |
| 245 | 'origin': headers.get('host', ''), |
| 246 | 'url': f'http://{headers["host"]}{path}', |
| 247 | } |
| 248 | |
| 249 | await send_json_response(send, response) |
| 250 | |
| 251 | |
| 252 | async def echo_status(scope: dict[str, Any], _receive: Receive, send: Send) -> None: |
nothing calls this directly
no test coverage detected