3

Flask seems to be not adding a slash at the url end before the get parameters in every case. But is doing it only in this case.

it changes /users?uid=1 to /users/?uid=1

After changing it to tha it even gives me a 404 error. "The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again."

Here's the code:

from flask import Flask, render_template, jsonify, Response, request
app = Flask(__name__)

@app.route("/users")
@app.route("/post")
@app.route("/bookmarks")
@app.route("/<target>")
def category_browser(target = ""):
    if(target != "" and target not in ['categories']):
        return render_template("404.html")
    else:
        return render_template("view.html")


if(__name__ == "__main__"):
    app.debug = True;
    app.run(port=int(80))
3
  • Seems to me that this is a chromium only bug. Commented Jul 7, 2015 at 0:08
  • 1
    As you noted try running this with another browser or command line tools such as wget/curl. Also see code.google.com/p/chromium/issues/detail?id=339054 Commented Jul 7, 2015 at 0:58
  • I can do that. But what about my users? Commented Jul 7, 2015 at 1:23

1 Answer 1

3

You had a stale cache entry in instance of Chromium when you had the route defined as @app.route("/users/"). You later changed to @app.route("/users") which Chrome still had it cached with the trailing /. Try accessing this simple example using incognito mode and see that /users?uid=1 remaining unchanged and that no 404 is reported. This is what happens when I first accessed it initially (using Chrome 42).

127.0.0.1 - - [07/Jul/2015 14:02:39] "GET /users?target=1 HTTP/1.1" 200 -

Then stopping that script (thanks for that complete almost self-contained example) and add @app.route("/users/") to the list of routes, below the original @app.route("/users/") route (to have a higher order of precedence so that Flask first trigger the redirect), i.e.:

@app.route("/users")
@app.route("/users/")

(Or simply remove the @app.route("/users") decorator)

Now try accessing the same page again in your incognito session, note that in your console:

127.0.0.1 - - [07/Jul/2015 14:04:11] "GET /users?target=1 HTTP/1.1" 301 -
127.0.0.1 - - [07/Jul/2015 14:04:11] "GET /users/?target=1 HTTP/1.1" 200 -

Ah, there's your redirect. Remove that extra line we just added, try going to /users?target=1 again, this is what happens:

127.0.0.1 - - [07/Jul/2015 14:07:22] "GET /users/?target=1 HTTP/1.1" 404 -

Chrome silently rewrites the URL to /users/?target=1 based on the cache entry in the incognito mode, and is reflected because only that URL is showing up on the Flask access log.

If you wish to support both methods, you have to do it this way:

@app.route("/users/")
@app.route("/users")

Then both access methods work:

127.0.0.1 - - [07/Jul/2015 14:08:49] "GET /users/?target=1 HTTP/1.1" 200 -
127.0.0.1 - - [07/Jul/2015 14:08:59] "GET /users?target=1 HTTP/1.1" 200 -

Rather than resulting in:

127.0.0.1 - - [07/Jul/2015 14:10:00] "GET /users?target=1 HTTP/1.1" 301 -
127.0.0.1 - - [07/Jul/2015 14:10:00] "GET /users/?target=1 HTTP/1.1" 200 -
Sign up to request clarification or add additional context in comments.

1 Comment

This answer fixed my identical issue in Django. Just add both url(r'/users', 'app.views.users') and url(r'/users/', 'app.views.users')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.