AWS API Gateway Malformed Lambda Response

六月ゝ 毕业季﹏ 提交于 2019-12-11 04:45:15

问题


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}"

def addnumbers(message, context):

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

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


回答1:


Ok buckle in for an answer.

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

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": "Whatever the input is you want the lambda to test" } }

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

I put this code within :

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.

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

It should now return with hello, randomname



来源:https://stackoverflow.com/questions/46672812/aws-api-gateway-malformed-lambda-response

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!