Correct http status code for resource which requires authorization

后端 未结 8 488
太阳男子
太阳男子 2021-01-29 23:51

There seems to be a lot of confusion about the correct http status code to return if the user tries to access a page which requires the user to login.

So basically what

8条回答
  •  无人共我
    2021-01-30 00:44

    As you point out, 403 Forbidden is explicitly defined with the phrase "Authorization will not help", but it is worth noting that the authors were almost certainly referring here to HTTP authorization (which will indeed not help as your site uses a different authorization scheme). Indeed, given that the status code is a signal to the user agent rather than the user, such a code would be correct insofar as any authorization the agent attempts to provide will not assist any further with the required authorization process (c.f. 401 Unauthorized).

    However, if you take that definition of 403 Forbidden literally and feel it is still inappropriate, perhaps 409 Conflict might apply? As defined in RFC 2616 §10.4.10:

       The request could not be completed due to a conflict with the current
       state of the resource. This code is only allowed in situations where
       it is expected that the user might be able to resolve the conflict
       and resubmit the request. The response body SHOULD include enough
       information for the user to recognize the source of the conflict.
       Ideally, the response entity would include enough information for the
       user or user agent to fix the problem; however, that might not be
       possible and is not required.
    

    There is indeed a conflict with the current state of the resource: the resource is in a "locked" state and such conflict can only be "resolved" through the user providing their credentials and resubmitting the request. The body will include "enough information for the user to recognize the source of the conflict" (it will state that they are not logged-in) and indeed will also include "enough information for the user or user agent to fix the problem" (i.e. a login form).

提交回复
热议问题