普通网友 2019-09-24 19:52
浏览 376
已采纳

无法读取JSON Cookie

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)

  • 写回答

1条回答 默认 最新

  • duan198123 2019-09-24 19:59
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题
  • ¥15 求大家看看Nonce如何配置
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改