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

后端 未结 8 1032
生来不讨喜
生来不讨喜 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: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.

提交回复
热议问题