Should I use HTTP 4xx to indicate HTML form errors?

前端 未结 4 1083
滥情空心
滥情空心 2020-12-19 04:33

I just spent 20 minutes debugging some (django) unit tests. I was testing a view POST, and I was expecting a 302 return code, after which I asserted a bunch database entitie

相关标签:
4条回答
  • 2020-12-19 04:41

    The POST returns 200 if you do not redirect. The 302 is not sent automatically in headers after POST request, so you have to send the header (https://docs.djangoproject.com/en/dev/ref/request-response/#django.http.HttpResponse) manually and the code does not relay on data of the form.

    The reason of the redirection back to the form (or whatever) with code 302 is to disallow browser to send the data repeatedly on refresh or history browsing.

    0 讨论(0)
  • 2020-12-19 04:48

    There doesn't appear to be an accepted answer, which to be honest, is a bit surprising. Form validation is such a cornerstone of web development that the fact that there is no response code to illustrate a validation failure seems like a missed opportunity. Particularly given the proliferation of automated testing. It doesn't seem practical to test the response by examining the HTML content for an error message rather than just testing the response code.

    I stick by my assertion in the question that 200 is the wrong response code for a request that fails business rules - and that 302 is also inappropriate. (If a form fails validation, then it should not have updated any state on the server, is therefore idempotent, and there is no need to use the PRG pattern to prevent users from resubmitting the form. Let them.)

    So, given that there isn't an 'approved' method, I'm currently testing (literally) with my own - 421. I will report back if we run into any issues with using non-standard HTTP status codes.

    If there are no updates to this answer, then we're using it in production, it works, and you could do the same.

    0 讨论(0)
  • 2020-12-19 05:03

    You are right that 200 is wrong if the outcome is not success.

    I'd also argue that a success-with-redirect-to-result-page should be 303, not 302.

    4xx is correct for client error. 422 seems right to me. In any case, don't invent new 4xx codes without registering them through IANA.

    0 讨论(0)
  • 2020-12-19 05:06

    It's obvious that some form POST requests should result in a 4xx HTTP error (e.g. wrong URL, lacking an expected field, failing to send an auth cookie), but mistyping passwords or accidentally omitting required fields are extremely common and expected occurrences in an application.

    It doesn't seem clear from any spec that every form invalidation problem must constitute an HTTP error.

    I guess my intuition is that, if a server sends a client a form, and the client promptly replies with a correctly-formed POST request to that form with all expected fields, a common business logic violation shouldn't be an HTTP error.

    The situation seems even less defined if a client-side script is using HTTP as a transport mechanism. E.g. if a JSON-RPC requests sends form details, the server-side function is successfully called and the response returned to the caller, seems like a 200 success.

    Anecdotally: Logging in with bad credentials yields a 200 from Facebook, Google, and Wikipedia, and a 204 from Amazon.

    Ideally the IETF would clear this up with an RFC, maybe adding an HTTP error code for "the operation was not performed due to a form invalidation failure" or expanding the definition of 422 to cover this.

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