flask-restful

Parsing a list of integers in flask-restful

倾然丶 夕夏残阳落幕 提交于 2019-12-05 10:12:51
I'm using the flask-restful , and I'm having trouble constructing a RequestParser that will validate a list of only integers. Assuming an expected JSON resource format of the form: { 'integer_list': [1,3,12,5,22,11, ...] # with a dynamic length } ... and one would then create a RequestParser using a form something like: from flask.ext.restful import reqparse parser = reqparse.RequestParser() parser.add_argument('integer_list', type=list, location='json') ... but how can i validate is an integer list? You can check types with isinstance, here you set the type to int (integer). This would work

How to send username:password to unittest's app.get() request?

你。 提交于 2019-12-05 01:24:32
This is part of my unit test in Flask-RESTful. self.app = application.app.test_client() rv = self.app.get('api/v1.0/{0}'.format(ios_sync_timestamp)) eq_(rv.status_code,200) Within the command line I could use curl to send the username:password to the service: curl -d username:password http://localhost:5000/api/v1.0/1234567 How do I achieve the same within my unit test's get() ? Since my get/put/post require authentication otherwise the test would fail. From RFC 1945, Hypertext Transfer Protocol -- HTTP/1.0 11.1 Basic Authentication Scheme ... To receive authorization, the client sends the user

Raising A Custom Error with Flask-Restful

时光毁灭记忆、已成空白 提交于 2019-12-04 19:02:21
问题 All, I'm trying to raise a custom error using Flask-Restful, following the docs. For testing purposes, I've defined and registered the errors dictionary exactly link in the docs: api = flask_restful.Api(app, errors=errors) . However, when I want to raise the custom error using (e.g.) abort(409) within the resource module, firebug reports: { "message": "Conflict", "status": 409 } This seems like the standard 409 error, nothing custom; from the docs, I would expect the custom error message- "A

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

ぃ、小莉子 提交于 2019-12-04 15:54:20
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(debug=True) Uploading a file with the following curl command works just fine. curl 127.0.0.1:5000/foo

TypeError on CORS for flask-restful

点点圈 提交于 2019-12-04 04:42:59
While trying the new CORS feature on flask-restful, I found out that the decorator can be only applied if the function returns a string. For example, modifying the Quickstart example : class HelloWorld(restful.Resource): @cors.crossdomain(origin='*') def get(self): return {'hello': 'world'} Throws: TypeError: 'dict' object is not callable Am I doing something wrong? matthewjaestokeley I recently came across this issue myself. @MartijnPieters is correct, decorators can't be called on single methods of the view. I created an abstract base class that contained the decorator list. The class that

Structure Flask-Restful API to use SQLAlchemy

回眸只為那壹抹淺笑 提交于 2019-12-03 20:25:21
问题 So I'm trying to make an API, using Flask-Restful, but all the examples I find put everything into one app.py file. I found information in the Flask-Restful docs explaining how to structure your API, but it doesn't include anything for using a database. I've posted what I've come up with, and it works if I hard-code some data, but when I import the db into users.py I get an error ImportError: cannot import name 'db' . So, what is the best way to structure an API to bring in your data from a

flask-RESTful vs flask-RESTless, which should be used, and when [closed]

房东的猫 提交于 2019-12-03 15:12:31
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . What are the key differences of these two libraries.In terms of usability, and features If you already have a huge number of model classes, using flask-restLESS makes perfect sense, right?. What are the features it would lack, if it would done with flask-restful. Or, what are

Flask-Restful POST fails due CSRF protection of Flask-WTF

帅比萌擦擦* 提交于 2019-12-03 09:43:07
问题 I am using normal flask web + flask-restful. So I need CSRF protection for web but not for REST. The moment I enable CsrfProtect(app) of flask-wtf , all my post unit tests for flask-restful return a 400. Is there a way to disable CSRF protection for REST services since they are coming from mobile handsets without session handling anyway, hence CSRF wouldn't make much sense. This is how I test it: rv = self.client.post('api/v1.0/verify-email', environ_base={'REMOTE_ADDR': '127.0.0.1'}, headers

Calling flask restful API resource methods

随声附和 提交于 2019-12-03 05:20:27
I'm creating an API with Flask that is being used for a mobile platform, but I also want the application itself to digest the API in order to render web content. I'm wondering what the best way is to access API resource methods inside of Flask? For instance if I have the following class added as a resource: class FooAPI(Resource): def __init__(self): # Do some things super(FooAPI, self).__init__() def post(self, id): #return something def get(self): #return something api = Api(app) api.add_resource(FooAPI, '/api/foo', endpoint = 'foo') Then in a controller I want: @app.route("/bar") def bar():

Nested validation with the flask-restful RequestParser

吃可爱长大的小学妹 提交于 2019-12-03 03:39:52
问题 Using the flask-restful micro-framework, I am having trouble constructing a RequestParser that will validate nested resources. Assuming an expected JSON resource format of the form: { 'a_list': [ { 'obj1': 1, 'obj2': 2, 'obj3': 3 }, { 'obj1': 1, 'obj2': 2, 'obj3': 3 } ] } Each item in a_list corresponds to an object: class MyObject(object): def __init__(self, obj1, obj2, obj3) self.obj1 = obj1 self.obj2 = obj2 self.obj3 = obj3 ... and one would then create a RequestParser using a form