I have a situation where I want to read the contents of a cookie in Go. However the contents of the cookie is in a JSON format. (Changing the format of the cookie is not an option)
For example the contents of the cookie might be:
{"id":"abc","data":"information","on_off":false}
In JavaScript I'm easily able to read the contents of the cookie and parse it.
With Go, on the other hand, when I try to read the cookie with r.Cookie('my_cookie')
I get the following error: http: named cookie not present
.
If I modify the cookie to a simple string, then it works as expected.
Does anyone know what to do in this case? Is it just not possible to read a such a cookie in Go?
When I use r.Header.Get["Cookie"]
, the output it returns does contain the cookie and it's JSON value (listed among all the other cookies)
JSON uses many characters not permitted in HTTP cookie values per the RFC - for example, double quotes, commas, and whitespace characters are not permitted. The eastiest way to transfer JSON data via cookie would probably be to Base64 encode it when setting the cookie, and Base64 decode it when reading the cookie, using the encoding/base64
package.