From 76ab88c9f58b9d122fc53d52ae5cb8fac02ae31e Mon Sep 17 00:00:00 2001 From: hansmi Date: Tue, 12 Feb 2019 18:47:33 +0100 Subject: [PATCH] callbacks/slack: Explicitly set Content-Type header (#51824) There are other chat systems with hook implementations more or less compatible with Slack, such as Rocket.Chat. The latter requires the Content-Type header to be set to "application/json" (the body is JSON). Signed-off-by: Michael Hanselmann --- changelogs/fragments/51824-slack-req-content-type.yaml | 4 ++++ lib/ansible/plugins/callback/slack.py | 7 ++++++- 2 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/51824-slack-req-content-type.yaml diff --git a/changelogs/fragments/51824-slack-req-content-type.yaml b/changelogs/fragments/51824-slack-req-content-type.yaml new file mode 100644 index 00000000000..651e86dd814 --- /dev/null +++ b/changelogs/fragments/51824-slack-req-content-type.yaml @@ -0,0 +1,4 @@ +minor_changes: + - >- + slack: Explicitly set Content-Type header to "application/json" for + improved compatibility with non-Slack chat systems diff --git a/lib/ansible/plugins/callback/slack.py b/lib/ansible/plugins/callback/slack.py index fc244097bf0..d0847f3ce9e 100644 --- a/lib/ansible/plugins/callback/slack.py +++ b/lib/ansible/plugins/callback/slack.py @@ -114,6 +114,10 @@ class CallbackModule(CallbackBase): 'variable.') def send_msg(self, attachments): + headers = { + 'Content-type': 'application/json', + } + payload = { 'channel': self.channel, 'username': self.username, @@ -127,7 +131,8 @@ class CallbackModule(CallbackBase): self._display.debug(data) self._display.debug(self.webhook_url) try: - response = open_url(self.webhook_url, data=data, validate_certs=self.validate_certs) + response = open_url(self.webhook_url, data=data, validate_certs=self.validate_certs, + headers=headers) return response.read() except Exception as e: self._display.warning(u'Could not submit message to Slack: %s' %