flask-restx icon indicating copy to clipboard operation
flask-restx copied to clipboard

Cannot use "/" route?

Open decentropy opened this issue 2 years ago • 5 comments

I am trying to use restx alongside a traditional flask route. However, I cannot get the default "/" route to work.

Running code below...

  • http://localhost:5000/test shows swagger doc
  • http://localhost:5000/hi loads 'hello'
  • http://localhost:5000 returns 404

How can I fix the 404, and handle root path?

from flask import Flask
from flask_restx import Api

app = Flask(__name__)

#restx api
api = Api(app, doc='/test/')

@app.route('/hi', methods=['GET'])
def home1():
    return 'hello'
    
@app.route('/', methods=['GET'])
def home2():
    return 'hello'

if __name__ == '__main__':
    app.run(debug=True)

decentropy avatar Jul 08 '22 21:07 decentropy

Figured it out...

I need to create api after the routes section

decentropy avatar Jul 08 '22 21:07 decentropy

Figured it out...

I need to create api after the routes section

why?

ericurse avatar Nov 15 '22 04:11 ericurse

Figured it out...

I need to create api after the routes section

Yes, you should either define it before creating the Api instance, or alternatively you can create your own Api sub class and overwrite the render_root method.

class MyApi(Api):
    def render_root(self) -> "Response":
        return "Root content"

There is a third option if you use blueprints, you can pop the second index from blueprint's deferred_functions which is the root.

blueprint = Blueprint("endpoint", __name__, template_folder="templates")
api = Api(blueprint)

# get rid of Api registered "/" root path
blueprint.deferred_functions.pop(2)

I'm not sure why it's necessary to add the root rule though. Would love to see it skips that step.

martincpt avatar Jan 30 '23 14:01 martincpt

I need to create api after the routes section

It took me a while but I came to the same conclusion.

In the CHANGELOG, for 1.1.0 version, it lists the following change as a bugfix:

  • Add a note to the docs that flask-restx always registers the root (/) path. [peter-doggart]

I don't know what bug it fixed but it feels like a regression. Previously I used version 1.0.3 and I could easily create a route for /. Maybe instead of "always registering the root path", we could change the code to register some default root route in case it wasn't defined by the user?

FrostyX avatar Apr 27 '23 20:04 FrostyX

There was no actual code change in 1.1.0 for this behaviour, it was simply an update to the docs by me to make it clearer that the root path gets registered. As far as I’m aware, this has always been flask-restx behaviour, as far back as 0.51.0 anyways.

There were some other minor changes to however in 1.0.5 due to underlying changes in how flask allows blueprints to be registered in 2.3. Are you having issues?

Unfortunately there is no easy fix due to how the url paths get generated which is fairly baked into how flask-restx works. However, it is easy to work around by creating the / root before you create your api at least.

On Thu, Apr 27, 2023 at 21:17, Jakub Kadlčík @.***(mailto:On Thu, Apr 27, 2023 at 21:17, Jakub Kadlčík < wrote:

I need to create api after the routes section

It took me a while but I came to the same conclusion.

In the CHANGELOG, for 1.1.0 version, it lists the following change as a bugfix:

  • Add a note to the docs that flask-restx always registers the root (/) path. [peter-doggart]

I don't know what bug it fixed but it feels like a regression. Previously I used version 1.0.3 and I could easily create a route for /. Maybe instead of "always registering the root path", we could change the code to register some default root route in case it wasn't defined by the user?

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>

peter-doggart avatar Apr 27 '23 20:04 peter-doggart