<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sccotliu</title>
    <description>The latest articles on DEV Community by Sccotliu (@sccotliu).</description>
    <link>https://dev.to/sccotliu</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1233167%2Fee13bb2e-6070-44e6-a5d8-b7e1d1c2cb7a.jpeg</url>
      <title>DEV Community: Sccotliu</title>
      <link>https://dev.to/sccotliu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sccotliu"/>
    <language>en</language>
    <item>
      <title>Non-GET Requests Returning 403 Error in Django Web Project</title>
      <dc:creator>Sccotliu</dc:creator>
      <pubDate>Thu, 14 Dec 2023 09:22:29 +0000</pubDate>
      <link>https://dev.to/sccotliu/non-get-requests-returning-403-error-in-django-web-project-g94</link>
      <guid>https://dev.to/sccotliu/non-get-requests-returning-403-error-in-django-web-project-g94</guid>
      <description>&lt;p&gt;During the development of a web project based on Django, I encountered a strange issue: all non-GET requests such as POST, PATCH, and DELETE that were previously working fine started returning a 403 error.&lt;/p&gt;

&lt;p&gt;Here are the basic details:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Django==4.0.10
djangorestframework==3.13.1
my restframework configuration in settings.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;REST_FRAMEWORK = {
    # "DEFAULT_PAGINATION_CLASS": "utility.rest_framework.paginations.Pagination",
    # "DEFAULT_PAGINATION_CLASS": "utility.rest_framework.paginations.PageNumberPaginationWithoutCount",
    "DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.LimitOffsetPagination",
    "PAGE_SIZE": 20,
    "DEFAULT_RENDERER_CLASSES": [
        # "rest_framework.renderers.JSONRenderer",
        "utility.rest_framework.renderers.CustomJSONRender",
        "rest_framework.renderers.BrowsableAPIRenderer",
    ],
    "DEFAULT_FILTER_BACKENDS": ["django_filters.rest_framework.DjangoFilterBackend"],
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
    "DEFAULT_AUTHENTICATION_CLASSES": [
        "rest_framework.authentication.BasicAuthentication",
        "rest_framework.authentication.SessionAuthentication",
    ],
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My API views are based on either ModelViewSet or APIView. The endpoints were working correctly in the morning, but after fixing a few performance-related issues, all non-GET requests started returning a 403 error. I checked the middleware and the   restframework   configuration in settings, but didn't find any apparent issues.&lt;/p&gt;

&lt;p&gt;While debugging the code, I discovered a method in the   rest_framework.views   module's   APIView   class with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def perform_authentication(self, request):
    """
    Perform authentication on the incoming request.

    Note that if you override this and simply 'pass', then authentication
    will instead be performed lazily, the first time either
      request.user   or   request.auth   is accessed.
    """
    request.user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every time I stepped into this method, the program would exit and raise a 403 error.&lt;/p&gt;

&lt;p&gt;To resolve the issue, I tried a workaround by manually extracting the CSRF token from the cookie and adding it to the request header. This workaround solved the problem, so I created a   BaseAPIView   class and made all views inherit from it.&lt;/p&gt;

&lt;p&gt;However, I'm still unsure about the root cause of the problem, which is quite frustrating.&lt;/p&gt;

&lt;p&gt;The possible cause could be Django's CSRF protection mechanism, which requires including the CSRF token in every non-GET request. It's possible that in the morning, the CSRF token was automatically included somewhere, but the changes made later caused the CSRF token to be missing.&lt;/p&gt;

&lt;p&gt;My solution of manually extracting the CSRF token from the cookie and adding it to the request header bypasses Django's CSRF protection mechanism. However, this approach is not recommended as it may introduce security vulnerabilities.&lt;/p&gt;

&lt;p&gt;I would appreciate your help in understanding the reason behind this issue.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
