【问题标题】:POST method not being invoked from the URL in API Gateway未从 API Gateway 中的 URL 调用 POST 方法
【发布时间】:2023-03-27 13:20:01
【问题描述】:

我有一个基本的flask-restx 应用程序 (main.py),如下所示:

ns = Namespace(
    "greetings",
    description="Get Greetings."
)
parser = reqparse.RequestParser()
parser.add_argument("name", type=str, help="name")


@ns.route('/restx/hello/')
class restx_hello(Resource):
    @ns.expect(parser, validate=True)
    def get(self):
        args = parser.parse_args()
        name = args['name']
        g = 'Greetings to you!'
        return 'Hello ' + name + '! I am from restx. ' + g

    @ns.expect(parser, validate=True)
    def post(self):
        args = parser.parse_args()
        name = args['name']
        return 'This is the POST method, ' + name 

从代码中可以看出,我在路由上同时调用了GETPOST 方法:/greetings/restx/hello/。我已经使用ServerlessAPI Gatewaylambda 上部署了这段代码。

serverless.yml 文件:

service: flask-api

provider:
  name: aws
  runtime: python3.7

functions:
  app:
    handler: wsgi_handler.handler
    events:
      - http: ANY /
      - http: 'ANY {proxy+}'

custom:
  wsgi:
    app: app.app
    pythonBin: python3
    packRequirements: false
  pythonRequirements:
    dockerizePip: non-linux

plugins:
  - serverless-wsgi
  - serverless-python-requirements

API Gateway 上,当我从仪表板单独测试GETPOST 方法时,它们会打印各自的输出。

GET 方法的屏幕截图

POST 方法的屏幕截图

我有此服务的以下 URL:https://abcdefgh.execute-api.eu-central-1.amazonaws.com/dev/{proxy+}

当我从浏览器调用 URL 本身时:https://abcdefgh.execute-api.eu-central-1.amazonaws.com/dev/greetings/restx/hello/?name=Alex 它总是调用GET 方法,如下所示:

永远不会调用POST 方法。我做错了什么?

【问题讨论】:

    标签: flask aws-lambda aws-api-gateway aws-serverless flask-restx


    【解决方案1】:

    在浏览器上,它总是使用GET方法。

    对于POST方法,你可以使用POSTMAN工具在:https://www.postman.com

    或者在终端上使用curl

    curl -X POST url
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-16
      • 2020-07-11
      • 2019-10-17
      • 2019-06-29
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 2019-06-11
      相关资源
      最近更新 更多