From 0ea5927b6a1064a343c2f3775b9d65aa0b601882 Mon Sep 17 00:00:00 2001 From: someposer Date: Fri, 3 Nov 2017 13:41:36 -0500 Subject: [PATCH] Adding Content-Type header to Webhook integrations to work correctly with services like https://ifttt.com/maker_webhooks which require a specific content type, like application/json. --- hc/api/models.py | 6 ++++++ hc/api/transports.py | 7 +++++-- hc/front/forms.py | 4 +++- templates/front/channels.html | 6 ++++++ templates/integrations/add_webhook.html | 16 ++++++++++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) diff --git a/hc/api/models.py b/hc/api/models.py index 3fa9d2ca..eca63700 100644 --- a/hc/api/models.py +++ b/hc/api/models.py @@ -321,6 +321,12 @@ class Channel(models.Model): parts = self.value.split("\n") return parts[2] if len(parts) > 2 else "" + @property + def content_type(self): + assert self.kind == "webhook" + parts = self.value.split("\n") + return parts[3] if len(parts) > 3 else "" + @property def slack_team(self): assert self.kind == "slack" diff --git a/hc/api/transports.py b/hc/api/transports.py index 8293a498..260c9634 100644 --- a/hc/api/transports.py +++ b/hc/api/transports.py @@ -164,8 +164,11 @@ class Webhook(HttpTransport): url = self.prepare(url, check, urlencode=True) if self.channel.post_data: + headers = {} + if self.channel.content_type: + headers["Content-Type"] = self.channel.content_type payload = self.prepare(self.channel.post_data, check) - return self.post(url, data=payload.encode("utf-8")) + return self.post(url, data=payload.encode("utf-8"), headers=headers) else: return self.get(url) @@ -230,7 +233,7 @@ class Pushbullet(HttpTransport): url = "https://api.pushbullet.com/v2/pushes" headers = { "Access-Token": self.channel.value, - "Conent-Type": "application/json" + "Content-Type": "application/json" } payload = { "type": "note", diff --git a/hc/front/forms.py b/hc/front/forms.py index 1617a5ef..2eb76934 100644 --- a/hc/front/forms.py +++ b/hc/front/forms.py @@ -65,9 +65,11 @@ class AddWebhookForm(forms.Form): post_data = forms.CharField(max_length=1000, required=False) + content_type = forms.CharField(max_length=1000, required=False) + def get_value(self): d = self.cleaned_data - return "\n".join((d["value_down"], d["value_up"], d["post_data"])) + return "\n".join((d["value_down"], d["value_up"], d["post_data"], d["content_type"])) phone_validator = RegexValidator(regex='^\+\d{5,15}$', diff --git a/templates/front/channels.html b/templates/front/channels.html index 946093ee..8c0cc719 100644 --- a/templates/front/channels.html +++ b/templates/front/channels.html @@ -80,6 +80,12 @@ {{ ch.post_data }} {% endif %} + {% if ch.content_type %} + + type  + {{ ch.content_type }} + + {% endif %} {% elif ch.kind == "pushbullet" %} API key diff --git a/templates/integrations/add_webhook.html b/templates/integrations/add_webhook.html index 1f388b34..3432bb08 100644 --- a/templates/integrations/add_webhook.html +++ b/templates/integrations/add_webhook.html @@ -105,6 +105,22 @@ {% endif %} +
+ +
+ + {% if form.content_type.errors %} +
+ {{ form.content_type.errors|join:"" }} +
+ {% endif %} +
+