From e99a7a557bedc90052561a13e3d7e6b7e81b24dd Mon Sep 17 00:00:00 2001 From: Justin Bassett Date: Mon, 7 Oct 2019 23:17:10 -0400 Subject: [PATCH] Properly handle the multipart request Actually read the documentation on Multipart: https://docs.aiohttp.org/en/stable/multipart.html#working-with-multipart --- custom_components/plex_webhooks/__init__.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/custom_components/plex_webhooks/__init__.py b/custom_components/plex_webhooks/__init__.py index 70a410b..967eb24 100644 --- a/custom_components/plex_webhooks/__init__.py +++ b/custom_components/plex_webhooks/__init__.py @@ -34,15 +34,16 @@ async def handle_webhook(hass, webhook_id, request): data = {} try: - body = await request.text() - # This is bad, but I don't see a better way to extract just the json... - raw_json = body[body.index('{'):body.rindex('}')+1] - data = json.loads(raw_json) if body else {} + reader = await request.multipart() + # https://docs.aiohttp.org/en/stable/multipart.html#aiohttp-multipart + while True: + part = await reader.next() + if part is None: + break + if part.name == 'payload': + data = await part.json() except ValueError: - logging.warn('Issue decoding webhook: ' + raw_json) - return None - except: - logging.debug('Ignoring webhook, must be a photo?') + logging.warn('Issue decoding webhook: ' + part.text()) return None data['playerUuid'] = data['Player']['uuid']