http status code for failed email send

2019-05-23 22:35发布

问题:

Consider an API call that creates a user. On success, the user is created and a confirmation email is sent. The response status code is 201.

If the user is not created, the response status code is 422.

What should the response status code be if user is created BUT sending the confirmation email failed?

回答1:

The core of the problem lies in mixing a RESTful resource creation and an RPC-style action. The creation of the user, in a RESTful world, would be the atomic operation and return 201 or 422 (or whatever error code you choose). Then the sending of the confirmation email might be another POST to /api/user/confirmationemailtask that then spins off the email process. When you combine the two you find the areas of ambiguity.

(moved comment to answer for posterity)