微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

未从API Gateway中的URL调用POST方法

如何解决未从API Gateway中的URL调用POST方法

我有一个基本的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 

代码中可以看出,我正在GET路由上同时调用POST/greetings/restx/hello/方法。我已经使用API GatewaylambdaServerless上部署了此代码

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方法的屏幕截图

enter image description here

POST方法的屏幕截图

enter image description here

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

当我通过浏览器以https://abcdefgh.execute-api.eu-central-1.amazonaws.com/dev/greetings/restx/hello/?name=Alex的形式调用URL本身时,它总是按以下方式调用GET方法

enter image description here

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

解决方法

在浏览器上,它始终使用GET方法。

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

或在终端上使用curl

curl -X POST url

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。