简体   繁体   English

Flask Python中的RESTful API示例

[英]Example of RESTful API in Flask Python

Can someone show me examples of making a RESTful API which uses database information in Flask? 有人可以向我展示一些使用Flask中的数据库信息制作RESTful API的示例吗? I have no idea how to implement POST, PUT and DELETE and I always get the 405 error where I can't use the method in url. 我不知道如何实现POST,PUT和DELETE,在无法在url中使用该方法的情况下,总是会收到405错误。

Have you add request method in your routing? 您在路由中添加了请求方法吗? you can following reference from: flask-restful 您可以参考以下参考: flask-restful

from flask import Flask, request
from flask_restful import Resource, Api

app = Flask(__name__)
api = Api(app)

class TodoSimple(Resource):
    def get(self):
        # do get something

    def put(self):
        # do put something

    def delete(self):
        # do delete something

    def post(self):
        # do post something

api.add_resource(TodoSimple, '/api/todo')

if __name__ == '__main__':
    app.run(debug=True)

in flask-restful the HTTP actions (GET, PUT, POST, DELETE) have their corresponding method in the resource class, so is just a matter of defining those method in the resource (with the corresponding parameter defined in the routing) 在烧瓶不稳定的情况下,HTTP操作(GET,PUT,POST,DELETE)在资源类中具有其相应的方法,因此只需在资源中定义这些方法即可(使用在路由中定义的相应参数)

I've also built a lightweight framework for building restful apis that makes it super easy to build apis. 我还构建了一个轻量级框架来构建静态API,这使得构建api非常容易。 You can take a look at the code to have an idea of how an API can be built, configured and run, and of course, build on top of it 您可以看一下代码,以了解如何构建,配置和运行API,当然还要在其之上构建

here's the code: https://github.com/sebastiandev/peach 这是代码: https : //github.com/sebastiandev/peach

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM