如何在 AWS lambda 中响应 non-latin 个字符?
How to response non-latin characters in AWS lambda?
10 月 12 日更新:
问题现已解决。有关详细信息,请参阅 aws 论坛中的 this post。
我写了一个nodejs函数,简单的用一些汉字来响应。但是它以错误的字符响应。
exports.handler = function(event, context) {
context.succeed('Hello 世界!');
};
函数结果变为:
"Hello ������������!"
我在写一个函数来解析一些中文网站并检索它们的页面标题时遇到了这个问题。我设法将它们转换为 utf-8(我使用 needle 作为请求),并且 console.log(title) 正确显示了这些汉字。但是 context.succeed() 的结果如上例所示。响应结果时这些non-latin个字符应该怎么处理?
如AWS Documentation - Programming Model (Node.js):
Indicates the Lambda function execution and all callbacks completed
successfully. Here's the general syntax:
context.succeed (Object result);
where
Object result
– provides the result of the function execution. The
result provided must be JSON.stringify
compatible. This parameter is
optional. You can call this method without any parameters (succeed()
)
or pass a null
value (succeed(null)
). If AWS Lambda fails to stringify
or encounters another error, an unhandled error is thrown, with the
X-Amz-Function-Error
response header set to Unhandled.
所以,你不能得到这样的结果:
Hello 世界!
因为字符串Hello 世界!
将被编码为JSON。所以,它将 return:
"Hello 世界!"
如果您在浏览器中看到使用 AWS Lambda 控制台:
"Hello ������!"
其实是有效的JSON,你只需要先解码再处理即可[=26] =]
尝试运行这个:
exports.handler = function (event, context) {
var jsonStr = JSON.stringify('Hello 世界!');
console.log(jsonStr);
console.log(JSON.parse(jsonStr));
context.succeed('Hello 世界!');
};
日志结果将是:
2015-08-07T12:49:54.888Z 12345678-90ab-cdef-1234-567890abcdef "Hello 世界!"
2015-08-07T12:49:54.889Z 12345678-90ab-cdef-1234-567890abcdef Hello 世界!
解码后,可以取回原来的字符串。
来自 AWS Support(2015 年 8 月 10 日):
Thank you for reaching out AWS Support with your question about
Lambda and UTF-8.
We are presently researching this issue as other customers have
brought this to our attention. There is no eta on when this will be
resolved or if this is something we can resolve.
10 月 12 日更新:
问题现已解决。有关详细信息,请参阅 aws 论坛中的 this post。
我写了一个nodejs函数,简单的用一些汉字来响应。但是它以错误的字符响应。
exports.handler = function(event, context) {
context.succeed('Hello 世界!');
};
函数结果变为:
"Hello ������������!"
我在写一个函数来解析一些中文网站并检索它们的页面标题时遇到了这个问题。我设法将它们转换为 utf-8(我使用 needle 作为请求),并且 console.log(title) 正确显示了这些汉字。但是 context.succeed() 的结果如上例所示。响应结果时这些non-latin个字符应该怎么处理?
如AWS Documentation - Programming Model (Node.js):
Indicates the Lambda function execution and all callbacks completed successfully. Here's the general syntax:
context.succeed (Object result);
where
Object
result
– provides the result of the function execution. The result provided must beJSON.stringify
compatible. This parameter is optional. You can call this method without any parameters (succeed()
) or pass anull
value (succeed(null)
). If AWS Lambda fails to stringify or encounters another error, an unhandled error is thrown, with theX-Amz-Function-Error
response header set to Unhandled.
所以,你不能得到这样的结果:
Hello 世界!
因为字符串Hello 世界!
将被编码为JSON。所以,它将 return:
"Hello 世界!"
如果您在浏览器中看到使用 AWS Lambda 控制台:
"Hello ������!"
其实是有效的JSON,你只需要先解码再处理即可[=26] =]
尝试运行这个:
exports.handler = function (event, context) {
var jsonStr = JSON.stringify('Hello 世界!');
console.log(jsonStr);
console.log(JSON.parse(jsonStr));
context.succeed('Hello 世界!');
};
日志结果将是:
2015-08-07T12:49:54.888Z 12345678-90ab-cdef-1234-567890abcdef "Hello 世界!"
2015-08-07T12:49:54.889Z 12345678-90ab-cdef-1234-567890abcdef Hello 世界!
解码后,可以取回原来的字符串。
来自 AWS Support(2015 年 8 月 10 日):
Thank you for reaching out AWS Support with your question about Lambda and UTF-8.
We are presently researching this issue as other customers have brought this to our attention. There is no eta on when this will be resolved or if this is something we can resolve.