Returns (status_code, parsed_json_or_None).
(url: str, data: dict)
| 89 | |
| 90 | |
| 91 | def http_post_json(url: str, data: dict) -> tuple: |
| 92 | """Returns (status_code, parsed_json_or_None).""" |
| 93 | try: |
| 94 | body = json.dumps(data).encode() |
| 95 | req = urllib.request.Request(url, data=body, method="POST") |
| 96 | req.add_header("Content-Type", "application/json") |
| 97 | with urllib.request.urlopen(req, timeout=10) as resp: |
| 98 | text = resp.read().decode() |
| 99 | try: |
| 100 | return resp.status, json.loads(text) |
| 101 | except json.JSONDecodeError: |
| 102 | return resp.status, text |
| 103 | except urllib.error.HTTPError as e: |
| 104 | text = e.read().decode() if e.fp else "" |
| 105 | try: |
| 106 | return e.code, json.loads(text) |
| 107 | except (json.JSONDecodeError, Exception): |
| 108 | return e.code, text |
| 109 | except Exception as e: |
| 110 | return 0, str(e) |
| 111 | |
| 112 | |
| 113 | async def create_room_ws(room_id: str): |