I've been studying about both AngularJS and Django recently. AngularJS promises to handle incoming cookie named 'XSRF-TOKEN' and to add a request header named 'X-XSRF-TOKEN' automatically. So I set django variables in settings.py:
CSRF_COOKIE_NAME = 'XSRF-TOKEN'
CSRF_HEADER_NAME = 'X-CSRF-TOKEN'
Obviously, it is not working as is implied here. So I had to install angular-cookies.js module to retrieve 'csrftoken' cookie and put its value in request header 'X-CSRF-TOKEN'. However, it's still not working. So what ever it is this time, it must be on server side.
HTML template:
<!DOCTYPE html>
<html>
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>youtube-dl</title>
<link href="/video/css/video.css" rel="stylesheet" type="text/css">
<script src="/js/angular-1.6.4.min.js" type="text/javascript"></script>
<script src="/js/angular-cookies.js" type="text/javascript"></script>
</head>
<body>
<div id="container" class="center" ng-app="vc" ng-controller="vc-ctl">
<img id="dl-icon" src="/video/logo.png"></img>
<form name="form">
{% csrftoken %}
<input class="url {{urlState}}" type="url" name="url" placeholder="https://" required autofocus ng-model="url">
<button class="btn btn1" ng-if="!form.url.$valid">
{{ buttonText }}
</button>
<button class="btn btn2 {{btn2State}}" ng-if="form.url.$valid" ng-click="click();">
{{ buttonText }}
</button>
</form>
</div>
<script src="/video/js/app.js" type="text/javascript"></script>
</body>
</html>
views.py:
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import json
from django.shortcuts import render
from django.http import HttpResponse
from .models import VideoTask
# Create your views here.
def index(request):
if request.method == 'GET':
return render(request, 'video/index.html')
stat = {
"full_path: ", request.get_full_path(),
"host: ", request.get_host(),
"port: ", request.get_port(),
"is_ajax: ", request.is_ajax(),
"is_secure: ", request.is_secure(),
"data: ", request.read(),
}
return HttpResponse(json.dumps(stat))
settings.py:
CSRF_COOKIE_NAME = 'csrftoken'
CSRF_HEADER_NAME = 'X-CSRF-TOKEN'
app.js:
var app = angular.module("vc", [
'ngCookies'
]);
app.run(function($rootScope, $http, $cookies) {
$rootScope.buttonText = "Submit URL";
$http.defaults.headers.post['X-CSRF-TOKEN'] = $cookies.csrftoken;
});
app.controller("vc-ctl", function($scope, $http, $cookies) {
$scope.cb_success = function(res) {
$scope.progList = res.data.progList;
};
$scope.cb_failure = function(res) {
console.log(res.status);
};
$scope.postUrl = "/wsgi/video/";
$scope.click = function() {
//$scope.btn2State = "hidden";
var data = encodeURIComponent($scope.url);
var cookie_csrf = $cookies.get('csrftoken');
console.log("Cookie: \"" + cookie_csrf + "\"");
console.log("POST {data: \"" + data + "\"}");
var req = {
method: 'POST',
url: $scope.postUrl,
headers: {
'X-CSRF-TOKEN': cookie_csrf,
},
data: {
url: data
}
};
$http(req).then($scope.cb_success, $scope.cb_failure);
};
$scope.update = function() {
$http.post($scope.postUrl, {"action": "echo"}).then($scope.cb_success, $scope.cb_failure);
};
});
Obviously I have {% csrftoken %} in my HTML form. When I check my data parcels in Firefox, I have both headers, 'Cookie' and 'X-CSRF-TOKEN'.