Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 使用post请求调用API时获得500响应_Python 3.x_Aws Lambda_Aws Api Gateway_Serverless Framework - Fatal编程技术网

Python 3.x 使用post请求调用API时获得500响应

Python 3.x 使用post请求调用API时获得500响应,python-3.x,aws-lambda,aws-api-gateway,serverless-framework,Python 3.x,Aws Lambda,Aws Api Gateway,Serverless Framework,我已经部署了一个集成API的无服务器函数。下面是代码 Serverless.yml handler.py 下面是我为api添加的调用lambda函数的权限 我用body请求调用了api,例如: { “名称”:“123” } 下面是日志 但它仍然返回500个内部错误。我搜索了不同的方法来获得解决方案,每个人都说,这只是因为API必须拥有调用lambda的权限。由于我已经提供了API的许可,我想知道具体的解决方案是什么? 如果有人能在这方面提供帮助,我们将不胜感激。这里的问题是您的身体: {

我已经部署了一个集成API的无服务器函数。下面是代码

Serverless.yml handler.py 下面是我为api添加的调用lambda函数的权限 我用body请求调用了api,例如: { “名称”:“123” }

下面是日志 但它仍然返回500个内部错误。我搜索了不同的方法来获得解决方案,每个人都说,这只是因为API必须拥有调用lambda的权限。由于我已经提供了API的许可,我想知道具体的解决方案是什么?
如果有人能在这方面提供帮助,我们将不胜感激。

这里的问题是您的身体:

{
    'statusCode': 200,
    'body': "Hello Post accepted",
    "content-type": "application/json"
}
它应该是(当您将头设置为json并传递普通文本时):


我已经检查并测试了您的无服务器框架代码,经过轻微修改后,我能够通过进行以下更改使设置正常工作:

handler.py

    import json

    def hello(event, context):

        postData = event['body']

    # Prepare a response dictionary as required by API Gateway
        response = {
            "statusCode": 200,
            "isBase64Encoded": False,
            "headers": {},
            "content-type": "application/json",
            "body": json.dumps(postData)
        }

        return response 
Serverless.yml

service: HelloWorld
provider:
    name: aws
    runtime: python2.7
    region: eu-west-1

    iamRoleStatements:
      - Effect: Allow
        Action:
          - dynamodb:Query
          - dynamodb:Scan
          - dynamodb:GetItem
          - dynamodb:PutItem
          - dynamodb:UpdateItem
          - dynamodb:DeleteItem
        Resource: "arn:aws:dynamodb:${opt:region, self:provider.region}:*:table/${self:provider.environment.DYNAMODB_TABLE}"
    logs:  true

    environment:
        DYNAMODB_TABLE: '${self:service}-${opt:stage, self:provider.stage}'

functions:
    hello:
        handler: handler.hello
        events:
          - http:       
              path: customers-details
              method: POST
              integration: LAMBDA # Add this line

resources:
  Resources:
    TodosDynamoDbTable:
      Type: 'AWS::DynamoDB::Table'
      DeletionPolicy: Retain
      Properties:
        TableName: CustomerTablev1
        AttributeDefinitions:

          - AttributeName: id
            AttributeType: S
          - AttributeName: CustomerName
            AttributeType: S
        KeySchema:
          -
            AttributeName: id
            KeyType: HASH
          - AttributeName: CustomerName
            KeyType: RANGE
        ProvisionedThroughput:
          ReadCapacityUnits: 1
          WriteCapacityUnits: 1
卷曲测试

请求:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"syumaK"}' --url https://z0uu17yp6f.execute-api.eu-west-1.amazonaws.com/dev/customers-details
{"body": "{\"name\": \"syumaK\"}", "headers": {}, "content-type": "application/json", "isBase64Encoded": false, "statusCode": 200}%     
响应:

curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"syumaK"}' --url https://z0uu17yp6f.execute-api.eu-west-1.amazonaws.com/dev/customers-details
{"body": "{\"name\": \"syumaK\"}", "headers": {}, "content-type": "application/json", "isBase64Encoded": false, "statusCode": 200}%     
要点是,在使用无服务器框架和AWS Lambda(Lambda proxy)时,必须在事件下的Serverless.yml中添加以下行:

          integration: LAMBDA

希望这有帮助

您是否从控制台部署了API网关资源?正如我前面提到的,我通过无服务器框架部署了Hi@AbdullahAlMasudTushar,您是否可以在向函数发送测试事件后附加日志?Hi@DhavalChaudhary,我已经用日志更新了问题。由于配置错误,密钥是执行失败:输出映射引用无效的方法响应:200这似乎表明api的响应映射有问题
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"name":"syumaK"}' --url https://z0uu17yp6f.execute-api.eu-west-1.amazonaws.com/dev/customers-details
{"body": "{\"name\": \"syumaK\"}", "headers": {}, "content-type": "application/json", "isBase64Encoded": false, "statusCode": 200}%     
          integration: LAMBDA