簡體   English   中英

flask-restful有一個get / <id>並在同一個類中使用json發布

[英]flask-restful having a get/<id> and post with json in the same class

如果#api.add_resource(User,'/ user /')行被取消注釋,而另一個api.add_resource是,則用戶的get方法有效。 相反的是使post方法有效。

如何讓這兩條路徑都能正常工作?

from flask import Flask, request
from flask.ext.restful import reqparse, abort, Api, Resource
import os
# set the project root directory as the static folder, you can set others.
app = Flask(__name__)
api = Api(app)

class User(Resource):

    def get(self, userid):
        print type(userid)
        if(userid == '1'):
            return {'id':1, 'name':'foo'}
        else:
            abort(404, message="user not found")

    def post(self):
        # should just return the json that was posted to it
        return request.get_json(force=True)

api.add_resource(User, '/user/')
# api.add_resource(User, '/user/<string:userid>')

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

Flask-Restful 支持為單個資源注冊多個URL 注冊User資源時,只需提供兩個URL:

api.add_resource(User, '/user/', '/user/<userid>')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM