flask-restful

how to have a single search API using path parameters (No form used)

穿精又带淫゛_ 提交于 2020-01-07 08:04:03
问题 I have been using this view for searching a word as: db refers mongo connection (just for ref) @app.route('/') def index(): return render_template('index.html') @app.route('/words-<word>', methods=['GET', 'POST']) def wordsearch(word): collection_name=word[0].lower()+'_collection' words=db[collection_name] data=words.find({'word':word}) return render_template('wordsearch.html',data=data) In index.html template I have been doing this to match this above url as: <script type="text/javascript">

Two Variable Urls using Flask-Restful

半腔热情 提交于 2020-01-06 23:48:08
问题 This seems like something that would come up a lot, but I can't find any documentation on it. I'm writing an api and I want urls to look like this: '/api/v1.0/restaurant/Name&Address' Using Flask-restful, I've defined the url as '/api/v1.0/restaurant/<name>&<address>' Werkzeug doesn't like this however and raises a BuildError in werkzeug/routing.py When I define the url, with add_resource, as '/api/v1.0/restaurant/<name>' and hard-wire the address, everything works fine. How do I define the

Two Variable Urls using Flask-Restful

为君一笑 提交于 2020-01-06 23:47:32
问题 This seems like something that would come up a lot, but I can't find any documentation on it. I'm writing an api and I want urls to look like this: '/api/v1.0/restaurant/Name&Address' Using Flask-restful, I've defined the url as '/api/v1.0/restaurant/<name>&<address>' Werkzeug doesn't like this however and raises a BuildError in werkzeug/routing.py When I define the url, with add_resource, as '/api/v1.0/restaurant/<name>' and hard-wire the address, everything works fine. How do I define the

Flask-PyMongo - init_app() missing 1 required positional argument: 'app'

假如想象 提交于 2019-12-26 05:39:13
问题 i am trying to initialise my db using flask-pymongo. But i get the following error, File "run.py", line 22, in app = create_app("config") File "run.py", line 11, in create_app mongo.init_app(app) TypeError: init_app() missing 1 required positional argument: 'app' run.py from flask import Flask from app import api_bp from db import mongo def create_app(config_filename): app = Flask(__name__) app.config.from_object(config_filename) app.register_blueprint(api_bp, url_prefix='/api') mongo.init

To implement a web socket feature with Flask-Restful (REST Api) on the server side

徘徊边缘 提交于 2019-12-26 03:46:32
问题 WORK DONE : I have implemented a REST API with Mongo DB (PyMongo driver) using Flask-Restful having one endpoint named “Users” consisting of GET, POST, PUT, DELETE My PUT method: def put(self, short_name ): coll = db.users user_info = request.get_json() print user_info #for debugging coll.update({'short_name': short_name'}, {"$set": user _info}) return Response(json_util.dumps(user _info), mimetype='application/json') The above PUT method accepts a short_name, updates the User database and

Catch-All URL in flask-restful

…衆ロ難τιáo~ 提交于 2019-12-24 09:35:18
问题 There is a Catch-All URL ability in Flask from flask import Flask app = Flask(__name__) @app.route('/', defaults={'path': ''}) @app.route('/<path:path>') def catch_all(path): return 'You want path: %s' % path if __name__ == '__main__': app.run() A little demonstration.. % curl 127.0.0.1:5000 # Matches the first rule You want path: % curl 127.0.0.1:5000/foo/bar # Matches the second rule You want path: foo/bar How can I have the same functionality in flask-restful ? 回答1: The comment posted by

Python Rate Limit class based view Flask

心已入冬 提交于 2019-12-24 00:44:04
问题 I'm following this example: http://flask-limiter.readthedocs.org/en/stable/#ratelimit-string app = Flask(__name__) limiter = Limiter(app, key_func=get_remote_address) class MyView(flask.views.MethodView): decorators = [limiter.limit("10/second")] def get(self): return "get" def put(self): return "put" My problem is that in example, the application, limiter and classes are defined in the same file in my case the application and limiter are defined in the same file but my classes live in a

POST csv/Text file using cURL

无人久伴 提交于 2019-12-23 08:54:13
问题 How can I send POST request with a csv or a text file to the server running on a localhost using cURL . I have tried curl -X POST -d @file.csv http://localhost:5000/upload but I get { "message": "The browser (or proxy) sent a request that this server could not understand." } My server is flask_restful API . Thanks a lot in advance. 回答1: There are many alternate ways to accomplish this. One way is I have used the following: curl -F ‘data=@<file_location>’ <URL> Eg. curl -F data=@data.csv

How to pass file path in a REST API ala Dropbox using Flask-RESTful?

狂风中的少年 提交于 2019-12-21 21:29:15
问题 Dropbox has a REST API that allows file upload using the following URL. (Reference) https://api-content.dropbox.com/1/files_put/<root>/<path>?param=val I want to replicate this API structure using Flask-RESTful. I have the following class. class File(restful.Resource): def put(self, fname): // do stuff here The class is then automatically mapped with the following code. app = Flask(__name__) api = restful.Api(app) api.add_resource(File, '/<string:fname>') if __name__ == '__main__': app.run

How to parse the POST argument to a REST service?

穿精又带淫゛_ 提交于 2019-12-21 10:19:14
问题 It seems I have another JSON problem, this time when posting to the REST service. I am using Flask-Restful . api.add_resource(Records, '/rest/records/<string:email>/<string:password>/<string:last_sync_date>') parser = reqparse.RequestParser() parser.add_argument('record_date', type=str) parser.add_argument('records', type=str) parser.add_argument('rating', type=str) parser.add_argument('notes', type=str) class Records(Resource): def post(self, email, password, last_sync_date): args = parser