I'm trying to set a cookie on the response using Express, but it's encoding the value. I'm setting a value with an equals sign, but on the client, it's coming through as %3D.
Any advice?
Thanks!
The commonly held belief is that cookie values must be URL-encoded, but this is a fallacy even though it is the de facto implementation. The original specification indicates that only three types of characters must be encoded: semicolon, comma, and white space.
We can check cookies by visiting localhost:3000/setcookie. This will show a message as cookies are added. We can check the cookies by visiting localhost:3000/getcookie. Now we can see that our cookies are added to the cookies object as shown above.
55951 – HTML5 specifies UTF-8 encoding for cookie values.
Now to use cookies with Express, we will require the cookie-parser. cookie-parser is a middleware which parses cookies attached to the client request object. To use it, we will require it in our index. js file; this can be used the same way as we use other middleware.
Argh, I found it!
http://expressjs.com/en/api.html#res.cookie
encode option needs to be set to String.
Express req.cookie
method accepts the third param as config, you can set the encoding method for cookie value in it. By default it uses encodeURLComponent
, so to prevent this, you can define a custom function to override it:
const myCookieEncode = function (val) {
return val;
};
...
res.cookie(cookieName, cookieValue, { encode: myCookieEncode })
Actually, it's by default encoding into 'HTML URL encoding' So in URL every special character have a code and '=' has a code '%3D'. So it is simply converting into that code
You can read more from here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With