Skip to content
Advertisement

React Native format cookie

I have a cookie that looks like this:

{"SSOsess":"300|c6dc6d60ebebf0891004353352f24b77","RTBk":"300|451706000c67a37dfe5bebe0b5d26278","ph02RPNCiscoASA":"application.api.randwebsite.com","ph03RPNCiscoASA":"application.client.randwebsite.com"}

Are there any libraries that could help me format the string to something such as:

'SSOsess=300|c6dc6d60ebebf0891004353352f24b77; RTBk=300|451706000c67a37dfe5bebe0b5d26278; ph02RPNCiscoASA=application.api.randwebsite.com; ph03RPNCiscoASA=application.client.randwebsite.com'

I’ve been looking through set-cookie-parser but doesn’t look it contains any useful functions.

Advertisement

Answer

Try this.

let cookie = JSON.stringify({"SSOsess":"300|c6dc6d60ebebf0891004353352f24b77","RTBk":"300|451706000c67a37dfe5bebe0b5d26278","ph02RPNCiscoASA":"application.api.randwebsite.com","ph03RPNCiscoASA":"application.client.randwebsite.com"})

var output = cookie.split(":").join("=").split(',').join("; ")
output = "'"+output.substring(1,output.length-1).replace(/"/g, "")+"'"

console.log(output)

//Output

'SSOsess=300|c6dc6d60ebebf0891004353352f24b77; RTBk=300|451706000c67a37dfe5bebe0b5d26278; ph02RPNCiscoASA=application.api.randwebsite.com; ph03RPNCiscoASA=application.client.randwebsite.com'
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement