From 0bdc4d728109bae6d2d4ed368519d135a1c1e2f5 Mon Sep 17 00:00:00 2001 From: "I. Ahmad" <54180221+nerdguyahmad@users.noreply.github.com> Date: Mon, 30 May 2022 17:48:36 +0500 Subject: [PATCH] Use json_or_text helper for parsing webhook responses --- discord/webhook/async_.py | 5 +---- discord/webhook/sync.py | 7 +++++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/discord/webhook/async_.py b/discord/webhook/async_.py index d62975f2a..23e8fe7c7 100644 --- a/discord/webhook/async_.py +++ b/discord/webhook/async_.py @@ -26,7 +26,6 @@ from __future__ import annotations import logging import asyncio -import json import re from urllib.parse import quote as urlquote @@ -181,9 +180,7 @@ class AsyncWebhookAdapter: url, response.status, ) - data = (await response.text(encoding='utf-8')) or None - if data and response.headers['Content-Type'] == 'application/json': - data = json.loads(data) + data = await json_or_text(response) remaining = response.headers.get('X-Ratelimit-Remaining') if remaining == '0' and response.status != 429: diff --git a/discord/webhook/sync.py b/discord/webhook/sync.py index e37424b22..55bfa82d6 100644 --- a/discord/webhook/sync.py +++ b/discord/webhook/sync.py @@ -178,8 +178,11 @@ class WebhookAdapter: response.status = response.status_code # type: ignore data = response.text or None - if data and response.headers['Content-Type'] == 'application/json': - data = json.loads(data) + try: + if data and response.headers['Content-Type'] == 'application/json': + data = json.loads(data) + except KeyError: + pass remaining = response.headers.get('X-Ratelimit-Remaining') if remaining == '0' and response.status_code != 429: