I read that "401 Unauthorized" code must be used when a user:
According to RFC, in both cases server must return 401
code. But I need to differentiate then in my ajax requests.
Anybody have a tip to solve this?
Note: I don't want to use 403 Forbidden
code, because in 403 "Authorization will not help"
, according to RFC.
401 Unauthorized is the status code to return when the client provides no credentials or invalid credentials. 403 Forbidden is the status code to return when a client has valid credentials but not enough privileges to perform an action on a resource.
The HTTP 402 Payment Required is a nonstandard response status code that is reserved for future use. This status code was created to enable digital cash or (micro) payment systems and would indicate that the requested content is not available until the client makes a payment.
The HyperText Transfer Protocol (HTTP) 401 Unauthorized response status code indicates that the client request has not been completed because it lacks valid authentication credentials for the requested resource.
The three status codes that felt the most appropriate are: 401 - Unauthorized. 403 - Forbidden. 404 - Not Found.
Unless you intend to use HTTP authentication, the correct response is 403 ("Forbidden").
A response code of 401 triggers the browser to display a password dialog box, and then resubmit the same request with a WWW-Authenticate
header with the password data that the user supplied. That's probably not the behavior you want.
Don't get too hung up on the explanations in the RFCs -- what you really need to pay attention to are the browser and search engine side effects of the various response codes.
As for the "Authorization will not help"
bit, in this case that is correct, since using HTTP authorization (which specifically means the WWW-Authenticate
header), in fact, will not help.
A 403 response tells the browser that the user does not have permission to make that request, and the browser should not attempt to collect authentication data and resubmit the request. That's exactly the response you're after.
I believe 403 is the right one. We may have to tune the language in the specification to make that clear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With