Koa: Set up Chinese characters for cookies

Created on 25 May 2016  路  2Comments  路  Source: koajs/koa

this.cookies.set('test', '鎴戞槸koajs')

sent error argument value is invalid to the cloud

TypeError: 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:53: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)

Most helpful comment

In order to include Chinese characters in cookie names of values, you just have to come up with an encoding scheme for the value. The cookies module just leaves this choice up to you, since there are various different methods of doing this (the two most popular being to use URL encoding of encoded octets or the base64 of come octets). A simple example of using URL encoding (where the characters are encoded as UTF-8):

this.cookies.set('test', encodeURIComponent('鎴戞槸koajs'))

You can then read it back using the reverse:

decodeURIComponent(this.cookies.get('test'))

All 2 comments

Valid HTTP header values (including cookies) are defined in RFC 7230 Section 3.2 to only include a limited selection of ASCII characters (not including Chinese characters). This is implemented in the NPM package cookies (used by Koa) here.

In order to include Chinese characters in cookie names of values, you just have to come up with an encoding scheme for the value. The cookies module just leaves this choice up to you, since there are various different methods of doing this (the two most popular being to use URL encoding of encoded octets or the base64 of come octets). A simple example of using URL encoding (where the characters are encoded as UTF-8):

this.cookies.set('test', encodeURIComponent('鎴戞槸koajs'))

You can then read it back using the reverse:

decodeURIComponent(this.cookies.get('test'))
Was this page helpful?
0 / 5 - 0 ratings

Related issues

wlingke picture wlingke  路  3Comments

tracker1 picture tracker1  路  3Comments

tvq picture tvq  路  4Comments

felixfbecker picture felixfbecker  路  5Comments

SteveCruise picture SteveCruise  路  3Comments