python - 如何在 AWS Lambda 函数中获取 GET 和 POST 参数

标签 python aws-lambda aws-api-gateway serverless-framework

我正在玩弄 AWS Lambda + API 网关 + 无服务器 (Python)。太棒了!

所以我想函数中的event参数保存了很多信息,包括HTTP Request信息

此外,我发现

queryStringParameters
body

是保存 GET 和 POST 参数的键。

"queryStringParameters": {
  "name": "me"
},

"body": "------WebKitFormBoundaryXAin8CB3c0fwFfAe\r\nContent-Disposition: form-data; name=\"sex\"\r\n\r\nmale\r\n------WebKitFormBoundaryXAin8CB3c0fwFfAe--\r\n",

如何从 body 键中获取哈希/字典?

谢谢

最佳答案

如果您想在 Lambda 中完全自由/完全透明,那么您可能想看看 Lambda 代理集成

import json

def endpoint(event, context):
# With the Lambda proxy integration, API Gateway maps the entire client request to the
# input event parameter of the backend Lambda function as follows:
# {
#     "resource": "Resource path",
#     "path": "Path parameter",
#     "httpMethod": "Incoming request's method name"
#     "headers": {Incoming request headers}
#     "queryStringParameters": {query string parameters }
#     "pathParameters":  {path parameters}
#     "stageVariables": {Applicable stage variables}
#     "requestContext": {Request context, including authorizer-returned key-value pairs}
#     "body": "A JSON string of the request payload."
#     "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
# }
    body = {}
    body["event"] = event

    # With the Lambda proxy integration, API Gateway requires the backend Lambda function
    # to return output according to the following JSON format:
    # {
    #     "isBase64Encoded": true|false,
    #     "statusCode": httpStatusCode,
    #     "headers": { "headerName": "headerValue", ... },
    #     "body": "..."
    # }
    response = {
        "statusCode": 200,
        "isBase64Encoded": False,
        "headers": {"x-test-header" : "foobar"},
        "body": json.dumps(body),
    }
    return response

在模板中

"paths": {
    "/{proxy+}": {
      "x-amazon-apigateway-any-method": {
        "parameters": [{
          "name": "proxy",
          "in": "path",
          "required": true,
          "type": "string"
        }],
        "produces": ["application/json"],
        "responses": {},
        "x-amazon-apigateway-integration": {
          "responses": {
            "default": {
              "statusCode": "200"
            }
          },
          "uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:xxxx:function:yyy/invocations",
          "passthroughBehavior": "when_no_match",
          "httpMethod": "POST",
          "cacheNamespace": "57w2aw",
          "cacheKeyParameters": [
            "method.request.path.proxy"
          ],
          "contentHandling": "CONVERT_TO_TEXT",
          "type": "aws_proxy"
        }
      }
    }
}

关于python - 如何在 AWS Lambda 函数中获取 GET 和 POST 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46098047/

相关文章:

python - 如何有效地将 numpy ndarray 转换为元组列表?

aws-lambda - 如何在 CloudFormation 中设置集群创建者以在 lambda 中重用它?

json - Lambda 函数空白 DynamoDB 值

gradle - 如何避免 AWS SAM 重建并重新上传代码不变的 gradle 函数?

amazon-web-services - AWS Cognito Oauth 范围中的标识符是什么?

Python 多处理,需要给出额外的参数

Python 2.7 - 使用多个字典的字符串替换

python - 如何使用 AWS APIGateway 和 Lambda 返回多个 cookie?

aws-cloudformation - 在 API Gateway 下/使用 CF 创建方法

python - 使用对象 dtype 的 ndarray 与 None 进行元素比较