{"errcode":44001,"errmsg":"empty media data, hint: [1655962096234893527769663], from ip: 222.72.xxx.246, more info at https://open.work.weixin.qq.com/devtool/query?e=44001"}
平台
Windows企业微信(企业微信群机器人)
语言:Python
出错原因
上传url和发送的url使用了相同的api
尝试方案
1、更新urllib3和requests的版本,并不管用
2、查看网上的在boundary后加\r\n,感觉太过复杂,而且官方文档并未使用该方案,感兴趣的可以看看,然而我这里成功的时候作出判断,这个方案行不通(在我遇到的这个情况下),附上地址(https://blog.youkuaiyun.com/xuexiaodong009/article/details/83081911)
另外网上的方案基本只有上述两类,如果下面的方法尝试后无效,可自行查看上面提到过的两种方案。
出错代码
def sent_to_work_wechat(file_path=None):
"""
上传文件至企业微信通告群
Parma file_path: 上传文件路径
"""
if not file_path:
# file_path = './2022-07-18-tonggao_log.txt'
file_path = f'{get_time_of_timestamp()}-tonggao_log.txt'
# 通告平台 通告数量 群机器人
robot_curl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=9df679e-1352-4663-bc22-69e82662c9c1"
upload_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=9df679e-1352-4663-bc22-69e82662c9c1&type=file"
data = {'file': open(file_path, 'rb')}
resp = requests.post(upload_url, files=data)
json_res = resp.json()
logger.info(f"企业微信机器人上传文件成功,file:{file_path}")
post_data = {
"msgtype": "file",
"file": {"media_id": json_res.get('media_id')}
}
headers = {'Content-Type': 'application/json'}
resp = requests.post(url=robot_curl, headers=headers, json=post_data)
logger.info(f"response: {resp.text}")
成功代码
def sent_to_work_wechat(file_path=None):
"""
上传文件至企业微信通告群
Parma file_path: 上传文件路径
"""
if not file_path:
# file_path = './2022-07-18-tonggao_log.txt'
file_path = f'{get_time_of_timestamp()}-tonggao_log.txt'
# 通告平台 通告数量 群机器人
robot_curl = "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=9df679e-1352-4663-bc22-69e82662c9c1"
upload_url = "https://qyapi.weixin.qq.com/cgi-bin/webhook/upload_media?key=9df679e-1352-4663-bc22-69e82662c9c1&type=file"
data = {'file': open(file_path, 'rb')}
resp = requests.post(upload_url, files=data)
json_res = resp.json()
logger.info(f"企业微信机器人上传文件成功,file:{file_path}")
post_data = {
"msgtype": "file",
"file": {"media_id": json_res.get('media_id')}
}
headers = {'Content-Type': 'application/json'}
resp = requests.post(url=robot_curl, headers=headers, json=post_data)
logger.info(f"response: {resp.text}")
区别
当时出问题的代码就在上传文件和发送文件的url上,因为当时把参数搞乱了(袍子傻笑:嗝嗝嗝咯咯)
所以如果出现了这个问题,请先查看常用参数是否正常,例如url