nodejs(koajs)设置中文cookie无效
来源:互联网 发布:淘宝运营规则是什么 编辑:程序博客网 时间:2024/06/09 17:04
通过koajs的cookies方法设置cookie时,写了中文的cookie值,一直失败。代码如下:
this.cookies.set('test', '我是koajs')
报错如下:
sent error argument value is invalid to the cloudTypeError: argument value is invalid at new Cookie (/Users/zyy/github/fete/node_modules/cookies/lib/cookies.js:110:11) at Cookies.set (/Users/zyy/github/fete/node_modules/cookies/lib/cookies.js:73:16) at Object.<anonymous> (/Users/zyy/github/fete/app.js:54:18) at next (native) at onFulfilled (/Users/zyy/github/fete/node_modules/co/index.js:65:19) at /Users/zyy/github/fete/node_modules/co/index.js:54:5 at Object.co (/Users/zyy/github/fete/node_modules/co/index.js:50:10) at converted (/Users/zyy/github/fete/node_modules/koa-convert/index.js:17:15) at dispatch (/Users/zyy/github/fete/node_modules/koa-compose/index.js:43:32) at next (/Users/zyy/github/fete/node_modules/koa-compose/index.js:44:18) at createGenerator (/Users/zyy/github/fete/node_modules/koa-convert/index.js:24:16) at next (native) at onFulfilled (/Users/zyy/github/fete/node_modules/co/index.js:65:19) at /Users/zyy/github/fete/node_modules/co/index.js:54:5 at Object.co (/Users/zyy/github/fete/node_modules/co/index.js:50:10) at Object.toPromise (/Users/zyy/github/fete/node_modules/co/index.js:118:63)
倒腾了半夜也没找到方案,其实是http协议的Header头有字符限制,下图是stackoverflow的解答,http的header字符集支持US-ASCII子集的字符集,故设置中文是'utf8'时就会报上面错误。
问题链接如下:
http://stackoverflow.com/questions/4400678/http-header-should-use-what-character-encoding
故解决方案:
把字符串转成base64即可
this.cookies.set('test', new Buffer('我是koajs').toString('base64'))
base64转到字符串
new Buffer(str, 'base64').toString();//str是base64编码的字符串
阅读全文
0 0
- nodejs(koajs)设置中文cookie无效
- nodejs 设置中文cookie无效
- KoaJs
- Servlet设置Cookie无效
- webView中设置cookie无效
- cookie设置中文问题
- NodeJs 发送request请求 设置Cookie
- ThinkPHP cookie 设置中文乱码
- nodejs开发中关于cookie及session的设置
- 经典Web安全缺陷(Cookie设置无效)
- Android Okhttp框架webView设置Cookie无效问题
- 服务端删除cookie: 响应头设置过期的时间+无效的cookie值
- nodejs创建cookie方法
- NodeJS获得Cookie
- [nodejs] cookie操作
- COOKIE无效的原因
- response.addCookie(cookie)无效
- cookie setMaxAge 无效
- 开始学习java
- HDFS
- Windows RabbitMQ服务端安装与配置详解
- Java面向对象编程
- 什么是分布式缓存框架以及分布式缓存框架的优缺点
- nodejs(koajs)设置中文cookie无效
- [教程] React Native基础实战(1)—— 制作一个简单的按钮
- 【剑指offer-解题系列(24)】二叉树中和为某一值
- eclipse添加代码提示
- 软件架构模式
- 【剑指offer-解题系列(21)】栈的压入、弹出序列
- 安卓错误:cannot be resolved or is not a field
- eclipse启动时内存溢出的解决办法
- 学习笔记TF015:加载图像、图像格式、图像操作、颜色