Custom error message json object with flask-restful

People tend to overuse abort(), while in fact it is very simple to generate your own errors. You can write a function that generates custom errors easily, here is one that matches your JSON: def make_error(status_code, sub_code, message, action): response = jsonify({ ‘status’: status_code, ‘sub_code’: sub_code, ‘message’: message, ‘action’: action }) response.status_code = status_code return … Read more

How to apply integration tests to a Flask RESTful API

Flask provides a test_client you can use in your tests: from source.api import app from unittest import TestCase class TestIntegrations(TestCase): def setUp(self): self.app = app.test_client() def test_thing(self): response = self.app.get(“https://stackoverflow.com/”) assert <make your assertion here> Flask Testing Docs

Python Flask-Restful POST not taking JSON arguments

junnytony’s answer gave me a hint, and I went ahead with this approach. get_json seems to have done the trick. from flask import Flask, jsonify, request from flask_restful import reqparse, abort, Api, Resource app = Flask(__name__) api = Api(app) #parser = reqparse.RequestParser() #parser.add_argument(‘username’, type=unicode, location=’json’) #parser.add_argument(‘password’, type=unicode, location=’json’) class HelloWorld(Resource): def post(self): json_data = request.get_json(force=True) … Read more

Flask RESTful cross-domain issue with Angular: PUT, OPTIONS methods

With the Flask-CORS module, you can do cross-domain requests without changing your code. from flask.ext.cors import CORS app = Flask(__name__) cors = CORS(app, resources={r”/api/*”: {“origins”: “*”}}) https://pypi.python.org/pypi/Flask-Cors https://github.com/corydolphin/flask-cors Update As Eric suggested, the flask.ext.cors module is now deprecated, you should rather use the following code: from flask_cors import CORS app = Flask(__name__) cors = CORS(app, … Read more

flask restful: passing parameters to GET request

Edit: reqparse is no longer the recommended way to do this with flask-restful!, but there is another example using marshmallow below. The reqparse object is deprecated. See the docs or the second example in this post for alternatives. Use reqparse. You can see another example in the flask-restful docs. It performs validation on the parameters … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)