<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom"><title>Guguweb - csrf</title><link href="https://www.guguweb.com/" rel="alternate"></link><link href="https://www.guguweb.com/feeds/csrf.atom.xml" rel="self"></link><id>https://www.guguweb.com/</id><updated>2022-01-14T21:06:19+01:00</updated><subtitle>Freelance developer and sysadmin</subtitle><entry><title>Integrate Axios with Django Rest Framework</title><link href="https://www.guguweb.com/2021/06/12/integrate-axios-with-django-rest-framework/" rel="alternate"></link><published>2021-06-12T12:34:09+00:00</published><updated>2022-01-14T21:06:19+01:00</updated><author><name>Augusto Destrero</name></author><id>tag:www.guguweb.com,2021-06-12:/2021/06/12/integrate-axios-with-django-rest-framework/</id><summary type="html">&lt;p&gt;Do you need to integrate the Axios HTTP client with Django Rest Framework? Then make sure to correctly configure the Django built-in Cross Site Request Forgery protection.&lt;/p&gt;
&lt;p&gt;TLDR: add these variables to your Django settings:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;CSRF_COOKIE_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;XSRF-TOKEN&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;CSRF_HEADER_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;HTTP_X_XSRF_TOKEN&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Axios has built-in support …&lt;/p&gt;</summary><content type="html">&lt;p&gt;Do you need to integrate the Axios HTTP client with Django Rest Framework? Then make sure to correctly configure the Django built-in Cross Site Request Forgery protection.&lt;/p&gt;
&lt;p&gt;TLDR: add these variables to your Django settings:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="n"&gt;CSRF_COOKIE_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;XSRF-TOKEN&amp;#39;&lt;/span&gt;
&lt;span class="n"&gt;CSRF_HEADER_NAME&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;HTTP_X_XSRF_TOKEN&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Axios has built-in support for CSRF protection, and this is the default configuration:&lt;/p&gt;
&lt;div class="highlight"&gt;&lt;pre&gt;&lt;span&gt;&lt;/span&gt;&lt;code&gt;&lt;span class="c1"&gt;// name of the cookie to use as a value for xsrf token&lt;/span&gt;
&lt;span class="nx"&gt;xsrfCookieName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;XSRF-TOKEN&amp;#39;&lt;/span&gt;

&lt;span class="c1"&gt;// name of the http header that carries the xsrf token value&lt;/span&gt;
&lt;span class="nx"&gt;xsrfHeaderName&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;X-XSRF-TOKEN&amp;#39;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;According to &lt;a href="https://docs.djangoproject.com/en/3.2/ref/settings/#csrf-header-name"&gt;Django documentation&lt;/a&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;blockquote&gt;
&lt;p&gt;"As with other HTTP headers in request.META, the header name received from the server is normalized by converting all characters to uppercase, replacing any hyphens with underscores, and adding an 'HTTP_' prefix to the name. For example, if your client sends a 'X-XSRF-TOKEN' header, the setting should be 'HTTP_X_XSRF_TOKEN'."&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;
&lt;/blockquote&gt;</content><category term="snippets"></category><category term="django"></category><category term="axios"></category><category term="rest"></category><category term="csrf"></category></entry></feed>