简体   繁体   中英

AWS API Gateway Malformed Lambda Response

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM