AWS API Gateway格式错误的Lambda响应 [英] AWS API Gateway Malformed Lambda Response

查看:149
本文介绍了AWS API Gateway格式错误的Lambda响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

The documentation states that the json should return containing a body,headers,and a status code all of which I have. However for whatever reason when I test it in API gateway it returns a malformed response.

这是其下面方法的输出.

This is the output of the method below it.

"{\" body \:200,\" headers \:{\" Content-type \: \"application/json \"},\"statusCode \":200}"

"{\"body\": 200, \"headers\": {\"Content-type\": \"application/json\"}, \"statusCode\": 200}"

def addnumbers(message, context):

    result = message['num1'] + 1
    print(result)
    resp = {
        "statusCode": 200,
         "body":  result,
        "headers": { "Content-type": "application/json"}
    }
    return (json.dumps(resp))

我当前传递的是num1 = 1,它没有给出任何更好的错误消息.任何指导将不胜感激.

I am currently passing in num1=1 and it doesn't give any better error message. Any guidance would be appreciated.

推荐答案

确定答案.

确保已在API中所需的任何资源上启用了代理集成.

Make sure you have proxy integration enabled on whatever resource you want in your API.

现在转到您的lambda.看一下我以前尝试传递num1.I的方式,我试图从事件"或消息中获取它.这是我绊倒的地方.还要注意(你不能用尸体获得东西) 相反,lambda的输入应该是这样的.

Now go to your lambda. Look at how I was previously trying to pass in num1.I was trying to get it from the "Event" or message. This is where I was tripping up. Also note (you can't do a get with a body) Rather the input to the lambda should be like this.

{ "queryStringParameters":{ "input":无论输入什么,您都希望lambda进行测试" } }

{ "queryStringParameters": { "input": "Whatever the input is you want the lambda to test" } }

因此,现在我们已经为lambda配置了测试,我们需要对lambda本身进行编码.

So now that we have our test configured for the lambda we need to code the lambda itself.

我将此代码放在:

def lambda_handler(事件,上下文):

def lambda_handler(event, context):

number = "Hello, " + event['queryStringParameters']['input']

out = {}
out['statusCode'] = 200
out['body'] = number

return (out)

现在,如果您进行测试,就可以了.

Now if you test it should be fine.

返回到API网关 在查询字符串"部分中,输入input = randomname

Go back to the API Gateway In the "Query Strings" Section put in input=randomname

现在应该以随机名称"hello"返回

It should now return with hello, randomname

这篇关于AWS API Gateway格式错误的Lambda响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆