HTTP 400 (bad request) for logical error, not malformed request syntax

后端 未结 8 799
生来不讨喜
生来不讨喜 2020-12-02 09:11

The HTTP/1.1 specification (RFC 2616) has the following to say on the meaning of status code 400, Bad Request (§10.4.1):

The request could not be unde

相关标签:
8条回答
  • 2020-12-02 09:54

    As of this time, the latest draft of the HTTPbis specification, which is intended to replace and make RFC 2616 obsolete, states:

    The 400 (Bad Request) status code indicates that the server cannot or will not process the request because the received syntax is invalid, nonsensical, or exceeds some limitation on what the server is willing to process.

    This definition, while of course still subject to change, ratifies the widely used practice of responding to logical errors with a 400.

    0 讨论(0)
  • 2020-12-02 09:55

    It could be argued that having incorrect data in your request is a syntax error, even if your actual request at the HTTP level (request line, headers etc) is syntactically valid.

    For example, if a Restful web service is documented as accepting POSTs with a custom XML Content Type of application/vnd.example.com.widget+xml, and you instead send some gibberish plain text or a binary file, it seems resasonable to treat that as a syntax error - your request body is not in the expected form.

    I don't know of any official references to back this up though, as usual it seems to be down to interpreting RFC 2616.

    Update: Note the revised wording in RFC 7231 §6.5.1:

    The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

    seems to support this argument more than the now obsoleted RFC 2616 §10.4.1 which said just:

    The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

    0 讨论(0)
提交回复
热议问题