mimic
mimic

Reputation: 5224

Flask-Restplus cannot find any route

I just started using this library so please correct me if I'm saying something wrong.

From my understanding, blueprint is kind of container for namespaces. For example I want to specify a blueprint for working with global objects (like projects):

from flask_restplus import Api
from flask import Blueprint
from app.global.projects import projects

global= Blueprint('global', __name__)

api = Api (global)

api.add_namespace(projects)

I register this blueprint for app:

from app.global import global
app.register_blueprint(global, '/global') #<== prefix

Then I specify a namespace with routes:

from flask_restplus import Namespace, Resource, fields

projects = Namespace('projects')

@projects.route('/projects')
class ProjectResource(Resource):
    def post(self):
        return {
            'num': 42
        }

So, from my understanding, if I send POST to the following url

127.0.0.1:5000/global/projects 

I will have {'num': 42}

But I have only 404. Where is an error?

Upvotes: 0

Views: 1031

Answers (1)

gtalarico
gtalarico

Reputation: 4689

Your code is not valid Python syntax (global keywords as a variable name) so, unfortunately, I could not test it.

But here is a similar working example with App, Blueprints, Api, and Namespace working together.

App

# run.py

from flask import Flask
from .api import blueprint

app = Flask(__name__)
app.register_blueprint(blueprint, url_prefix='')
app.run(debug=True)

Blueprint

# api.py

from flask import Blueprint
from flask_restplus import Api, Namespace, Resource

blueprint = Blueprint('api', __name__, url_prefix='/api')
api = Api(blueprint)
global_namespace = Namespace('global', path='/global')
api.add_namespace(global_namespace)


@global_namespace.route('/projects')
class ProjectResource(Resource):
    def get(self):
        return {'num': 42}

Test

$ python run.py
$ ...
$  curl http://127.0.0.1:5000/api/global/projects
{
   "num": 42
}

Upvotes: 2

Related Questions