@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中对此进行解析?
答案 0 :(得分:1)
首先无需获取request.body
。您似乎正在发布标准表单数据,其中包含一个包含JSON数据的payload
字段。所以就得到:
data = json.loads(request.POST['payload'])