From 5cc38311add222a89887386e63fdc34770b0560a Mon Sep 17 00:00:00 2001 From: Aman Tiwari Date: Mon, 2 Dec 2019 23:53:16 +0530 Subject: [PATCH] Added support for 'application/x-www-form-urlencoded' Currently this library supports only 'application/json' as content-type, This PR aims in adding support to the other content-type supported by Github for webhooks i.e. 'application/x-www-form-urlencoded'. This support makes python-github-webhook fully compatible with the github webhooks. Signed-Off-By: Shibani Mahapatra --- github_webhook/webhook.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/github_webhook/webhook.py b/github_webhook/webhook.py index d6addf9..d0d019e 100644 --- a/github_webhook/webhook.py +++ b/github_webhook/webhook.py @@ -2,6 +2,7 @@ import hashlib import hmac import logging +import json import six from flask import abort, request @@ -58,7 +59,12 @@ def _postreceive(self): abort(400, "Invalid signature") event_type = _get_header("X-Github-Event") - data = request.get_json() + content_type = _get_header('content-type') + + if content_type == "application/x-www-form-urlencoded": + data = json.loads(request.form.to_dict(flat=True)["payload"]) + else: + data = request.get_json() if data is None: abort(400, "Request body must contain json") @@ -121,7 +127,6 @@ def _format_event(event_type, data): except KeyError: return event_type - # ----------------------------------------------------------------------------- # Copyright 2015 Bloomberg Finance L.P. #