我有这个命令
curl -X POST --data-urlencode "payload={\"channel\": \"#test\", \"username\": \"kuhkuh\", \"text\": \"This is posted to #test\", \"icon_emoji\": \":ghost:\"}" https://hooks.slack.com/services/123/123/123
我试图使用python创建它,所以我可以在我的项目中使用它
import requests
import json
class SlackWrapper:
def __init__(self):
pass
@classmethod
def post_message_to_slack(self, err):
url = 'https://hooks.slack.com/services/123/123/123'
payload = {
'channel' : '#test',
'username' : 'kuhkuh',
'message' : err,
'icon_emoji' : ':ghosts:'
}
try:
alert = requests.post(url, data=payload, headers={'Content-Type': 'application/json'})
print(alert.status_code, ' - ', alert.text, ' - ', json.dumps(payload))
except Exception as e:
print(e)
SlackWrapper.post_message_to_slack("testing error message requests")
问题是,我不断收到此错误
<Response [400]>
我在哪里弄错了?
您正在尝试将数据作为字典发送到服务器。
而是尝试使用请求POST json属性将其作为json数据发送
payload = {
"channel" = '#test',
"username" = 'kuhkuh',
"message" = err,
"icon_emoji" = ':ghosts:'
}
alert = requests.post(url, json = payload)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句