3

ampersand(&) replaced by %26 value.

I set the cookie value TEST&123&223 in code, but in a browser, cookie value replaced by TEST%26123%26223.

I am trying to set cookie value in angular code. I have used third party library https://www.npmjs.com/package/ngx-cookie-service.

Is there a way to tell the browser, don't decode the string value in cookies?

Narasimha A
  • 319
  • 5
  • 14

2 Answers2

0

I think you should encode your data before storing it (In JavaScript it would be encodeURI('&') however if you use some other language on your server it could be different command but the idea stays the same).

Then you should decode cookie (decodeURI(string)) when reading the value.

Sergey Mell
  • 7,780
  • 1
  • 26
  • 50
0

The browser does not have anything to do with your issue : TEST%26123%26223 is an encoded value. It's the 3rd party library you're using that is encoding the value of your cookie.

    let cookieString: string = encodeURIComponent( name ) + '=' + encodeURIComponent( value ) + ';';

(extracted from library's github repository : https://github.com/7leads/ngx-cookie-service/blob/master/lib/cookie-service/cookie.service.ts)

Encoding cookie value is not a bad thing, it is actually recommended : Should cookie values be URL encoded? If you want to retrieve the original value you'll need to decode it (the library you use is decoding the cookie value in the get function).

jota3
  • 5,389
  • 2
  • 13
  • 22