Answers for "convert cookie string to js object"

0

convert cookies to json javascript

let cookie = "referer=example.com/post?id=22;bcomID=8075; subreturn=example&fuzzy=true&ct=null&autobounce=true; JSESSIONID=6D20570E1EB; mbox=session"; // sample cookies
let output = {};
cookie.split(/s*;s*/).forEach(function(pair) {
  pair = pair.split(/s*=s*/);
  output[pair[0]] = pair.splice(1).join('=');
});
let json = JSON.stringify(output, null, 4);
console.log(json);
Posted by: Guest on September-05-2021
-1

js parse cookie string

const parseCookie = str =>
  str
    .split(';')
    .map(v => v.split('='))
    .reduce((acc, v) => {
      acc[decodeURIComponent(v[0].trim())] = decodeURIComponent(v[1].trim());
      return acc;
    }, {});
Posted by: Guest on November-04-2021

Code answers related to "convert cookie string to js object"

Code answers related to "Javascript"

Browse Popular Code Answers by Language