From 0f23b9e3910f5a894b61ae6f8560461ad9876f9d Mon Sep 17 00:00:00 2001 From: X <2465124+broferek@users.noreply.github.com> Date: Sun, 21 Sep 2025 20:28:03 +0200 Subject: [PATCH] Force Content-type header to application/json if is_pre740 is false (#10832) * Force Content-type header to application/json if is_pre740 is false * Remove response variable from fail_json module * Add a missing blank line to match pep8 requirement * Add changelog fragment of issue #10796 * Rename fragment section * Improve fragment readability Co-authored-by: Felix Fontein --------- Co-authored-by: ludovic Co-authored-by: Felix Fontein --- .../fragments/10796-rocketchat-force-content-type.yml | 2 ++ plugins/modules/rocketchat.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) create mode 100644 changelogs/fragments/10796-rocketchat-force-content-type.yml diff --git a/changelogs/fragments/10796-rocketchat-force-content-type.yml b/changelogs/fragments/10796-rocketchat-force-content-type.yml new file mode 100644 index 0000000000..96ca116e62 --- /dev/null +++ b/changelogs/fragments/10796-rocketchat-force-content-type.yml @@ -0,0 +1,2 @@ +bugfixes: + - rocketchat - fix message delivery in Rocket Chat >= 7.5.3 by forcing ``Content-Type`` header to ``application/json`` instead of the default ``application/x-www-form-urlencoded`` (https://github.com/ansible-collections/community.general/issues/10796, https://github.com/ansible-collections/community.general/pull/10796). diff --git a/plugins/modules/rocketchat.py b/plugins/modules/rocketchat.py index 79dbaa77f2..bd3862ac0a 100644 --- a/plugins/modules/rocketchat.py +++ b/plugins/modules/rocketchat.py @@ -202,14 +202,17 @@ def build_payload_for_rocketchat(module, text, channel, username, icon_url, icon return payload -def do_notify_rocketchat(module, domain, token, protocol, payload): +def do_notify_rocketchat(module, domain, token, protocol, payload, is_pre740): if token.count('/') < 1: module.fail_json(msg="Invalid Token specified, provide a valid token") rocketchat_incoming_webhook = ROCKETCHAT_INCOMING_WEBHOOK % (protocol, domain, token) - response, info = fetch_url(module, rocketchat_incoming_webhook, data=payload) + headers = None + if not is_pre740: + headers = {'Content-type': 'application/json'} + response, info = fetch_url(module, rocketchat_incoming_webhook, data=payload, headers=headers) if info['status'] != 200: module.fail_json(msg="failed to send message, return status=%s" % str(info['status'])) @@ -256,7 +259,7 @@ def main(): is_pre740 = True payload = build_payload_for_rocketchat(module, text, channel, username, icon_url, icon_emoji, link_names, color, attachments, is_pre740) - do_notify_rocketchat(module, domain, token, protocol, payload) + do_notify_rocketchat(module, domain, token, protocol, payload, is_pre740) module.exit_json(msg="OK")