resthttp-status-codes

RESTful status code for a request that cannot be satisfied because of a dependency issue


My payment endpoint which accepts a POST should deny requests when the user does not have any payment methods configured. What is the correct HTTP status code in this case?

What is the correct HTTP status code to be raised when the system itself cannot reach the state asked for by the request and another request (creating a payment method) must be completed first?

I looked at 428 Precondition Required, but that seems to have more to do with the headers than the state of the system.


Solution

  • I would go simply with 400 Bad Request. If you need more specific instructions or hints in the response, you can return a body, which will indicate the exact nature of the error.

    You don't need to assign a specific HTTP error code to all your internal business-related error cases. Indeed this is probably not possible anyway.

    The specification on 400: https://www.rfc-editor.org/rfc/rfc7231#section-6.5.1

    Relevant quotes from there:

    indicates that the server cannot or will not process the request due to something that is perceived to be a client error

    And about the 4xx codes in general:

    the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition