Getting json body in aws Lambda via API gateway

前端 未结 4 769
鱼传尺愫
鱼传尺愫 2020-12-02 16:40

I\'m currently using NodeJS to build a bot on AWS lambda via AWS Api Gateway and I\'m running into an issue with POST requests and JSON data. My api uses \'Use Lambda Proxy

4条回答
  •  失恋的感觉
    2020-12-02 17:10

    I am using lambda with Zappa; I am sending data with POST in json format:

    My code for basic_lambda_pure.py is:

    import time
    import requests
    import json
    def my_handler(event, context):
        print("Received event: " + json.dumps(event, indent=2))
        print("Log stream name:", context.log_stream_name)
        print("Log group name:",  context.log_group_name)
        print("Request ID:", context.aws_request_id)
        print("Mem. limits(MB):", context.memory_limit_in_mb)
        # Code will execute quickly, so we add a 1 second intentional delay so you can see that in time remaining value.
        print("Time remaining (MS):", context.get_remaining_time_in_millis())
    
        if event["httpMethod"] == "GET":
            hub_mode = event["queryStringParameters"]["hub.mode"]
            hub_challenge = event["queryStringParameters"]["hub.challenge"]
            hub_verify_token = event["queryStringParameters"]["hub.verify_token"]
            return {'statusCode': '200', 'body': hub_challenge, 'headers': 'Content-Type': 'application/json'}}
    
        if event["httpMethod"] == "post":
            token = "xxxx"
        params = {
            "access_token": token
        }
        headers = {
            "Content-Type": "application/json"
        }
            _data = {"recipient": {"id": 1459299024159359}}
            _data.update({"message": {"text": "text"}})
            data = json.dumps(_data)
            r = requests.post("https://graph.facebook.com/v2.9/me/messages",params=params, headers=headers, data=data, timeout=2)
            return {'statusCode': '200', 'body': "ok", 'headers': {'Content-Type': 'application/json'}}
    

    I got the next json response:

    {
    "resource": "/",
    "path": "/",
    "httpMethod": "POST",
    "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "deflate, gzip",
    "CloudFront-Forwarded-Proto": "https",
    "CloudFront-Is-Desktop-Viewer": "true",
    "CloudFront-Is-Mobile-Viewer": "false",
    "CloudFront-Is-SmartTV-Viewer": "false",
    "CloudFront-Is-Tablet-Viewer": "false",
    "CloudFront-Viewer-Country": "US",
    "Content-Type": "application/json",
    "Host": "ox53v9d8ug.execute-api.us-east-1.amazonaws.com",
    "Via": "1.1 f1836a6a7245cc3f6e190d259a0d9273.cloudfront.net (CloudFront)",
    "X-Amz-Cf-Id": "LVcBZU-YqklHty7Ii3NRFOqVXJJEr7xXQdxAtFP46tMewFpJsQlD2Q==",
    "X-Amzn-Trace-Id": "Root=1-59ec25c6-1018575e4483a16666d6f5c5",
    "X-Forwarded-For": "69.171.225.87, 52.46.17.84",
    "X-Forwarded-Port": "443",
    "X-Forwarded-Proto": "https",
    "X-Hub-Signature": "sha1=10504e2878e56ea6776dfbeae807de263772e9f2"
    },
    "queryStringParameters": null,
    "pathParameters": null,
    "stageVariables": null,
    "requestContext": {
    "path": "/dev",
    "accountId": "001513791584",
    "resourceId": "i6d2tyihx7",
    "stage": "dev",
    "requestId": "d58c5804-b6e5-11e7-8761-a9efcf8a8121",
    "identity": {
    "cognitoIdentityPoolId": null,
    "accountId": null,
    "cognitoIdentityId": null,
    "caller": null,
    "apiKey": "",
    "sourceIp": "69.171.225.87",
    "accessKey": null,
    "cognitoAuthenticationType": null,
    "cognitoAuthenticationProvider": null,
    "userArn": null,
    "userAgent": null,
    "user": null
    },
    "resourcePath": "/",
    "httpMethod": "POST",
    "apiId": "ox53v9d8ug"
    },
    "body": "eyJvYmplY3QiOiJwYWdlIiwiZW50cnkiOlt7ImlkIjoiMTA3OTk2NDk2NTUxMDM1IiwidGltZSI6MTUwODY0ODM5MDE5NCwibWVzc2FnaW5nIjpbeyJzZW5kZXIiOnsiaWQiOiIxNDAzMDY4MDI5ODExODY1In0sInJlY2lwaWVudCI6eyJpZCI6IjEwNzk5NjQ5NjU1MTAzNSJ9LCJ0aW1lc3RhbXAiOjE1MDg2NDgzODk1NTUsIm1lc3NhZ2UiOnsibWlkIjoibWlkLiRjQUFBNHo5RmFDckJsYzdqVHMxZlFuT1daNXFaQyIsInNlcSI6MTY0MDAsInRleHQiOiJob2xhIn19XX1dfQ==",
    "isBase64Encoded": true
    }
    

    my data was on body key, but is code64 encoded, How can I know this? I saw the key isBase64Encoded

    I copy the value for body key and decode with This tool and "eureka", I get the values.

    I hope this help you. :)

提交回复
热议问题