0

I have a json object data like this and I want to set it as cookie:

enter image description here

I convert the this object to string via JSON.stringify(). And then when I want to set that new string text as Cookie to my Cookies area , setting processing does not work. I mean I can not write it as cookie value. I think there is an error during stringify. What is your solutions? Do you have any recommend me to set it as cookie value?

Pure data :

{"alias":"TestName","name":"Test","image":"sadasdasd3213","modules":["Community","InternalCommunication","Interest","Event","SelfService","FileSharing","SkillSet"],"id":2,"userAgreements":[{"id":"BA3909CC-2393-4CB9-AA52-178E2001A06B","url":"https://www.google.com.tr/docs/abcd.pdf","displayName":"Çerez Aydınlatma Metni"},{"id":"834657C3-0802-471C-8699-41A4D76977FA","url":"https://www.google.com.tr/docs/abcd.pdf","displayName":"Üyelik Sözleşmesi ve Kullanım Koşulları"},{"id":"27F11B55-4427-4C66-953C-70627E781B09","url":"https://www.google.com.tr/docs/abcd.pdf","displayName":"Aydınlatma Metni"}],"isConnectedToSso":true,"alertCount":0,"portal":{"portalLogo":"21321asfdasf3","id":3,"companyId":2,"portalName":"LIFE","portalLink":"https://www.google.com.tr/"},"isOtpProcess":true,"fontColor":"313384","backgroundColor":"313384","surveyLastDayValue":7}

example code :

   const testData = JSON.stringify(response.singleData)
   localStorage.setItem("alias", JSON.stringify(response.singleData)); // it works as local storage data
   Cookies.set('czn', JSON.stringify({data : response.singleData}) , { expires: 1 }) // js-cookie package , it does not work
   setCookie('alias', testData,{ path: '/' }); // react-cookie package, it does not work
Emre Sert
  • 318
  • 6
  • 19

1 Answers1

0

I would advise against using cookies as a storage medium for a larger JSON string as cookies have a restrictive size limit in modern browsers (4096 characters per domain) - you might be hitting up against that.

As the MDN web docs put it:

Cookies were once used for general client-side storage. While this made sense when they were the only way to store data on the client, modern storage APIs are now recommended. Cookies are sent with every request, so they can worsen performance (especially for mobile data connections). Modern APIs for client storage are the Web Storage API (localStorage and sessionStorage) and IndexedDB.

Rhys Mills
  • 187
  • 7