flask-restful

How to apply integration tests to a Flask RESTful API

How to apply integration tests to a Flask RESTful API Question: [As per https://stackoverflow.com/a/46369945/1021819, the title should refer to integration tests rather than unit tests] Suppose I’d like to test the following Flask API (from here): import flask import flask_restful app = flask.Flask(__name__) api = flask_restful.Api(app) class HelloWorld(flask_restful.Resource): def get(self): return {‘hello’: ‘world’} api.add_resource(HelloWorld, ‘/’) …

Total answers: 5

Flask-restful API Authorization. Access current_identity inside decorator

Flask-restful API Authorization. Access current_identity inside decorator Question: I use flask-restful to create my APIs. I have used flask-jwt for enabling authentication based on JWT. Now I need to do authorization. I have tried putting my authorization decorator. test.py (/test api) from flask_restful import Resource from flask_jwt import jwt_required from authorization_helper import authorized_api_user_type class Test(Resource): …

Total answers: 3

RuntimeError: working outside of application context

RuntimeError: working outside of application context Question: app.py from flask import Flask, render_template, request,jsonify,json,g import mysql.connector app = Flask(__name__) **class TestMySQL():** @app.before_request def before_request(): try: g.db = mysql.connector.connect(user=’root’, password=’root’, database=’mysql’) except mysql.connector.errors.Error as err: resp = jsonify({‘status’: 500, ‘error’: “Error:{}”.format(err)}) resp.status_code = 500 return resp @app.route(‘/’) def input_info(self): try: cursor = g.db.cursor() cursor.execute (‘CREATE TABLE …

Total answers: 4

flask restful: passing parameters to GET request

flask restful: passing parameters to GET request Question: I want to create a resource that supports GET request in following way: /bar?key1=val1&key2=val2 I tried this code, but it is not working app = Flask(__name__) api = Api(app) class BarAPI(Resource): def get(key1, key2): return jsonify(dict(data=[key1, key2])) api.add_resource(BarAPI, ‘/bar’, endpoint=’bar’) Thanks! Asked By: Rugnar || Source Answers: …

Total answers: 4

Python Flask-Restful POST not taking JSON arguments

Python Flask-Restful POST not taking JSON arguments Question: I am very new to Flask (& Flask-Restful). My problem : json arguments for a POST is getting set to NONE (not working). I am able to take arguments from the form-data, using POSTMAN plugin for chrome. But, when i switch to raw (& feed a json), …

Total answers: 4

'400 Bad Request' when post json in Flask

'400 Bad Request' when post json in Flask Question: My application is very simple, #@csrt.exempt @app.route(‘/preorders/json’, methods=[‘POST’]) def json_create_preorders(): #print request print ‘test’ #print request.json print request.mimetype print request.json print ‘aaa’,request.get_json(force=True) print request.json[‘product_id’] if not request.json or not ‘product_id’ in request.json or not ‘customer_name’ in request.json or not ‘customer_phone’ in request.json: abort(400) preorder=Preorder(request.json[‘customer_name’],request.json[‘customer_phone’],request.json[‘product_id’]) db.session.add(preorder) db.session.commit() …

Total answers: 3

Flask-RESTful custom routes other than GET,PUT,POST,DELETE

Flask-RESTful custom routes other than GET,PUT,POST,DELETE Question: In Flask-RESTful we add an api route like the below api.add_resource(CuteKitty,’/api/kitty’) class CuteKitty(Resource): def get(self): return {} def post(self): return {} def put(self): return {} def delete(self): return None, 204 so that GET /api/kitty –> to CuteKitty.get() method; like this for all HTTP verbs Lets say that I …

Total answers: 3

How to use Flask-Cache with Flask-Restful

How to use Flask-Cache with Flask-Restful Question: How do I use Flask-Cache @cache.cached() decorator with Flask-Restful? For example, I have a class Foo inherited from Resource, and Foo has get, post, put, and delete methods. How can I can invalidate cached results after a POST? @api.resource(‘/whatever’) class Foo(Resource): @cache.cached(timeout=10) def get(self): return expensive_db_operation() def post(self): …

Total answers: 6

Flask: Creating objects that remain over multiple requests

Flask: Creating objects that remain over multiple requests Question: I’ve been able to create objects that are created at every request from this link: http://flask.pocoo.org/docs/appcontext/#locality-of-the-context. I’m actually creating an API based off of http://blog.miguelgrinberg.com/post/designing-a-restful-api-using-flask-restful. I want to be able to load an object once and just have it return a processed response rather than it …

Total answers: 2

Flask-RESTful API: multiple and complex endpoints

Flask-RESTful API: multiple and complex endpoints Question: In my Flask-RESTful API, imagine I have two objects, users and cities. It is a 1-to-many relationship. Now when I create my API and add resources to it, all I can seem to do is map very easy and general URLs to them. Here is the code (with …

Total answers: 2