企业微信机器人python脚本执行报错-‘errcode‘: 40008, ‘errmsg‘: ‘Warning: wrong json format. invalid message type

企业微信机器人python脚本执行报错-‘errcode‘: 40008, ‘errmsg‘: ‘Warning: wrong json format. invalid message type错误内容{‘errcode’:40008,‘errmsg’:‘Warning:wrongjsonformat.invalidmessagetype,hint:[1596176563_47_d9bbe040d5a640ea75f8625e35783c76],fromip:61.183.117.38,moreinfoathttps://open.work.weixin.qq.com/devtool/query?e=40008’}查看官网错误代码意义40008 不合法

大家好,又见面了,我是你们的朋友全栈君。

错误内容

{‘errcode’: 40008, ‘errmsg’: ‘Warning: wrong json format. invalid message type, hint: [1596176563_47_d9bbe040d5a640ea75f8625e35783c76], from ip: 61.183.117.38, more info at https://open.work.weixin.qq.com/devtool/query?e=40008’}

查看官网错误代码意义

40008 不合法的msgtype参数 合法的msgtype取值,参考:消息类型

原因

企业微信机器人的消息虽然是“text”类型的,但是post发送过去的头部是“application/json”,是json格式的,查看python的request中的post方法:

def post(url, data=None, json=None, **kwargs):
    r"""Sends a POST request. :param url: URL for the new :class:`Request` object. :param data: (optional) Dictionary, list of tuples, bytes, or file-like object to send in the body of the :class:`Request`. :param json: (optional) json data to send in the body of the :class:`Request`. :param \*\*kwargs: Optional arguments that ``request`` takes. :return: :class:`Response <Response>` object :rtype: requests.Response """

    return request('post', url, data=data, json=json, **kwargs)

所以在python 的request中,不能使用data,data的数据只能是字典,列表或者元组。
而json= 发送的是json的数据,所以这里需要使用json

requests.post(url=URL, headers=HEADERS, json=Data, verify=False)

附链接:

Zabbix Basic -zabbix 使用python脚本调用群企业微信机器人将告警信息发送至“企业微信”群中,并@指定人员(提醒指定人员查看)

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/132721.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号