How to resolve the Django-Postman CSRF Token error?
This is a quick tutorial to resolve the CSRF error you receive in Postman while testing your Django application.
Step 1: Run the Django server and login into the admin page in the browser. Right-click anywhere on the webpage and click Inspect option.
Debug toolbox will open in your browser.
Step 2: Click on the network tab. It will start recording all the activities happening on the webpage.
Step 3: Reload the webpage. (Don’t close the inspect tab). Different entries will populate in the network tab. Click on admin entry.
Step 4: Go to the Headers tab. Scroll down and you will see Cookie inside Request Headers. Inside the cookie, you’ll find “csrftoken”. Copy/paste the token for future use.
Step 5: Go to the Postman application. Create a new request and fill the Url. In my case, it was “localhost:8000/dialogflow”. It can be different “localhost:8000” in your case. Click on cookies button.
Step 6: “Manage Cookies” tab will open. At the top, you’ll find “Add a domain” option. Type “localhost” in the domain and click “Add”.
Step 7: Click on the “Add Cookie” button.
Step 8: Some default cookie values will populate in the tab.
Step 9: Remove the “Cookie=value” option and paste the csrf token you copied earlier. Key will be csrftoken.
Step 10: Click the Save button. Your localhost domain will have 1 cookie. Close the “Manage Cookies” tab.
Step 11: Go to the “Headers” tab and add the token there also. Key will be “X-CSRFToken”.
Step 12: And that’s it. Send the request and you must receive the response.
Congratulations on making it to the end.
Keep Learning :)