RESTful Login Failure: Return 401 or Custom Response

后端 未结 3 1914
夕颜
夕颜 2020-12-13 03:31

This is a conceptual question.

I have a client (mobile) application which needs to support a login action against a RESTful web service. Because the web service is

3条回答
  •  [愿得一人]
    2020-12-13 03:57

    401 should be sent only when the request needs authorization header field and authorization fails. Since the Login API doesn't require authorization, hence 401 is the wrong error code in my opinion

    As per the standard here https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

    *10.4.2 401 Unauthorized

    The request requires user authentication. The response MUST include a WWW-Authenticate header field (section 14.47) containing a challenge applicable to the requested resource. The client MAY repeat the request with a suitable Authorization header field (section 14.8). If the request already included Authorization credentials, then the 401 response indicates that authorization has been refused for those credentials. If the 401 response contains the same challenge as the prior response, and the user agent has already attempted authentication at least once, then the user SHOULD be presented the entity that was given in the response, since that entity might include relevant diagnostic information. HTTP access authentication is explained in "HTTP Authentication: Basic and Digest Access Authentication" [43].*

提交回复
热议问题