首页 > 解决方案 > AWS Lambda 函数附加字符作为返回

问题描述

我有 2 个 Lambda 函数 [A 和 B]。函数 A 进行计算并返回 2 个字符串。请注意,我也尝试过返回一个字符串。当单独调用该函数时,返回的是正确的预期字符串。

如果我在函数 B 中调用函数 A,则返回的是正确的字符串,但每边都添加了字符。

函数 A1(返回两个字符串):

def handler(event, context):

    strings = {
           "first_string": "This is the first string",
           "second_string": "This is the second string"
    }

    return strings

函数 A2(返回一个字符串):

def handler(event, context):

    string = "This is a string"

    return string

在另一个 Lambda 函数中调用 A1:

return_strings = functionA1(event, context)
print(return_strings[0])
print(return_strings[1])

>>> 341 #expected This is the first string
>>> 8 #expected This is the second string

在另一个 Lambda 函数中调用 A2:

return functionA2(event, context)

>>> b'\"This is a string\"' #expected This is a string

知道返回中可能会编码什么 - 它与从另一个 Lambda 函数调用有关吗?单独调用 A1/A2 会产生预期回报。

谢谢!

标签: pythonamazon-web-servicesprintingreturnaws-lambda

解决方案


发现问题了!在读取 JSON 响应之前需要解码:

load = json.loads(response['Payload'].read().decode("utf-8"))

推荐阅读