Which requests are safe to retry
IDEMPOTENT_METHODS = {"GET", "PUT", "DELETE"}
def should_retry(method, status_code):
return method.upper() in IDEMPOTENT_METHODS and status_code >= 500
for method, status in [("GET", 503), ("PUT", 500), ("POST", 503), ("GET", 404)]:
print(f"{method} {status}: retry={should_retry(method, status)}")
GET 503: retry=True
PUT 500: retry=True
POST 503: retry=False
GET 404: retry=False