-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Status code 403 and text from 401 error #9692
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hello everyone :) Can you tell me, please, can anyone help? |
Hey @TooMuchInLove ! 👋 Thanks for the awesome work on DRF — I’ve run into something that might be worth discussing. 🧩 The Issue "Authentication credentials were not provided." However, the status code returned is 403 Forbidden, rather than the expected 401 Unauthorized. Here’s an example response I’m seeing: { With this response: 🔍 What's Happening? www_authenticate_realm = authenticate_header(self.request) So essentially: This creates a confusing mismatch: the client gets a 403, but the error message is clearly about missing authentication (which by REST/API standards, typically maps to 401). 💡 Workaround I Used if response.status_code == status.HTTP_403_FORBIDDEN: Yes, I know — not the most elegant solution 😅. But it helps clarify the response for clients. 🛠️ Suggestion I’d be happy to help contribute a PR or test enhancement around this behavior! Thanks again for the fantastic work you all do 🙌 |
@sanyamk23 this answer looks mostly LLM-generated. Please be mindful about about your generative AI use. In particular, please avoid generating extra-verbose response that don't add much to the discussion (more than 50% of your message repeats content from earlier messages). Also, please ensure your code snippets are wrpped in code fences. PS: we don't have an official policy but I found the Python dev guide's a reasonable starting point |
In my viewpoint Bruno is being exceptionally generous here. |
https://github.com/encode/django-rest-framework/blob/78e97074e7c823ef9c693e4b63ac1e5c6e41ff81/rest_framework/views.py#L491C1-L518C29
Hey guys!
Such a situation has arisen. When I tried to execute a request in my API (being unauthorized), I received a corresponding message stating that I could not do this without authorization.
But I noticed that the text of the message in the response is from error 401, and the error code is from 403.
DRF version
djangorestframework >= 3.14, < 3.15
.Problem description:
The whole point is that
BasicAuthentication
has an implementation of theauthenticate_header
method (this method is not implemented in theSessionAuthentication
class, it simply inherits fromBaseAuthentication'). And at the key moment of the program execution, the
APIViewclass (
ViewSetinherits from
APIView) and the
get_authenticate_headermethod receive a tuple of the
BasicAuthenticationand
SessionAuthenticationelements/classes passed to
DEFAULT_AUTHENTICATION_CLASSES (settings.py).Next, the first class from this tuple is strictly taken, and in turn the method
authenticate_header
is used (in the case of theBasicAuthentication
class, the specific value is returned, and theSessionAuthentication
class returns `None').Example (Response):
Is this a mistake or was it originally intended that way (and can it be fixed at the library level)?
Here's how I managed to solve my problem, but it looks like a crutch:
The text was updated successfully, but these errors were encountered: