检查发送到 webhook URL 是否成功

Check if sending to webhook URL is successful

我的 models.py

中有通过 webhook 发送数据的这一部分
for webhook in webhooks:
   requests.post(webhook.url, json=message):

只要数据发送不成功,整个过程就会失败,因为某些原因,例如给定的 URL 不是有效的 webhook URL。有什么方法可以避免进程中断,或者检查数据发送是否成功?

您可以像这样将调用包装在 try-catch 中:

for webhook in webhooks:
    try:
        r = requests.post(webhook.url, json=message)
    except Exception as e:
        print(e)
        # Do something here
    else:
        if r.status_code == 200:
            print('OK')
        else:
            print(f'Not OK : {r.text}')