【问题标题】:Check if sending to webhook URL is successful检查发送到 webhook URL 是否成功
【发布时间】:2021-02-23 13:30:40
【问题描述】:

我的 models.py 中有这部分通过 webhook 发送数据

for webhook in webhooks:
   requests.post(webhook.url, json=message):

每当数据发送不成功时,整个过程都会失败,例如给定的 URL 不是有效的 webhook URL。有什么办法可以避免进程中断,或者检查数据发送是否成功?

【问题讨论】:

    标签: django django-rest-framework webhooks


    【解决方案1】:

    您可以像这样将您的呼叫包装在 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}')
    

    【讨论】:

      猜你喜欢
      • 2012-11-30
      • 2011-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      相关资源
      最近更新 更多