[英]Slack interactive message: POST response is not in json format
@csrf_exempt
def slack(request):
print("Testing slack")
if request.method == 'POST':
print('request', str(request.body))
webhook_url = 'xxxxxxxx'
text = "Would you recommend it to customers?"
request = unquote(unquote(request.body.decode(encoding='ascii')))
print('url', request)
slack_data = {
"attachments": [
{
"fallback": "Would you recommend it to customers?",
"title": request,
"callback_id": "comic_1234_xyz",
"color": "#3AA3E3",
"attachment_type": "default",
"actions": [
{
"name": "recommend",
"text": "Recommend",
"type": "button",
"value": "recommended"
}
],
}
]
}
test = slack_data
print('slack_data', type(slack_data))
response = requests.post(
webhook_url, data=json.dumps(test),
headers={'Content-Type': 'application/json'}
)
return HttpResponse("New comic book alert!")
在此str(request.body)中,我得到如下输出:b'payload =%7B%22type%22%3A%22互动消息%22%2C%
所以我使用unquote(unquote(request.body.decode(encoding ='ascii')))对其进行了编码,并且我能够以这种格式获取有效负载:
payload={ "here I got all details of POST message" }
如何在Json中解析此内容?
无需首先获取request.body
。 看起来您正在发布标准表单数据,其中包含包含JSON数据的payload
字段。 所以就得到:
data = json.loads(request.POST['payload'])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.