0

i have some cookies on my page, and i need to save it and load by page refresh. Saving is working normally, but when i try to refresh. I get "HTTP Error 400. The size of the request headers is too long", i have 7-8 cookie files in cookies and their summary size is about ~ 18kb. i need to save an object in cookie its too big, and i split it to array. Then i write it into cookieStore (it works correct)

$scope.cookieDataSavePrepare = function(data) {
            var answerArray = JSON.stringify(data).match(/(.|[\r\n]){1,2048}/g);
            for (var i = 0; i < answerArray.length; i++) {
                $cookieStore.put('dataPart' + i, answerArray[i]);
            }
            $cookieStore.put('dataPartsNumber', answerArray.length);
        };
        $scope.cookieDataLoadPrepare = function() {
            var dataLoad = [];
            for (var i = 0; i < $cookieStore.get('dataPartsNumber'); i++) {
                var file = 'dataPart' + i;
                dataLoad.push($cookieStore.get(file));
            };
            return dataLoad.join('');
        };
        $scope.checkData=function() {
            $window.localStorage.setItem('checkData', $scope.cookieDataLoadPrepare());
        }

        $scope.saveTestsToCookieStore = function () {
            $scope.cookieDataSavePrepare($scope.data);
            $cookieStore.put('answeredQuestions',JSON.stringify($scope.answeredTests));
            $cookieStore.put('questionStopped',$scope.currentQN);
        }

How can i manage it?

inatoff
  • 151
  • 2
  • 12
  • 1
    If you don't need that much cookies on server-side, save them in localStorage? Or even [pouchDB](http://pouchdb.com/). Therefore [the size of all cookies should not exceed 4093 bytes.](http://stackoverflow.com/a/4604212/2308005) – Medet Tleukabiluly Jul 01 '15 at 05:31
  • use localstorage reference link below http://www.w3schools.com/html/html5_webstorage.asp – Siddharth Pandey Jul 01 '15 at 05:32
  • localstorage is not suitable, i need cookies( – inatoff Jul 01 '15 at 05:34
  • 1
    @inatoff why? Cookies aren't a mechanism for storing large amounts of data, as you already found out. – robertklep Jul 01 '15 at 05:36
  • If you need data on the client side use localstorage. If you need user or session specific data on the server use a session. If you need data on both ends, either use both, or save it on server and send it to client on each request, not the other way around. – Mikk3lRo Jul 01 '15 at 05:40

1 Answers1

0

Use localStorag. Or pouchDB. Therefore 50 cookies per domain, and 4093 bytes per domain.

Why?

Max url length is de facto limit of 2000 characters
But your data is 18kb is 18000 bytes which exceed 4093 bytes
1 char = 2 bytes, so 18000 / 2 = 9000 chars which exceed max url length

Community
  • 1
  • 1
Medet Tleukabiluly
  • 11,662
  • 3
  • 34
  • 69