ctx.message assignment of Chinese or Other language(such as Japanese) will throw an error: TypeError [ERR_INVALID_CHAR]: Invalid character in statusMessage
ShiliangLuo opened this issue · comments
I want to assign ctx.message to specific message, but koa throw an error. See: https://codesandbox.io/s/quiet-thunder-q7po0?file=/src/index.js
If I change the order of the sentence:
ctx.message = '中文';
ctx.body = 'hello world';
the program will run successfully, but the result of ctx.message is not changed. Puzzled!
maybe you can avoid this error by this way:
ctx.message = new Buffer("中文").toString("base64");
ctx.body = { code: 0, data: ctx.body, msg: Buffer(ctx.message, "base64").toString() };
Japanese is also no error
See: link
THX, I'll try. But, what's the reason?
@ShiliangLuo 这似乎是node的问题,
执行ctx.message = '中文';
时会设置 res.statusMessage = '中文';
对应代码在
Lines 111 to 115 in 8c1dd48
node中对statusMessage
做了限制,需要满足正则表达式/[^\t\x20-\x7e\x80-\xff]/
,代码在
下面这个issue有提到这个问题
nodejs/node#28797
@ShiliangLuo 这似乎是node的问题,
执行
ctx.message = '中文';
时会设置res.statusMessage = '中文';
对应代码在
Lines 111 to 115 in 8c1dd48
node中对
statusMessage
做了限制,需要满足正则表达式/[^\t\x20-\x7e\x80-\xff]/
,代码在下面这个issue有提到这个问题 nodejs/node#28797
嗯,后来我找到原因了
Unfortunately this has little to do with Koa as Koa delegates status message assignment to node:http.res.statusMessage
.