2

I am working with hardware machines whose firmware cannot be changed. I am supposed to make a server from them the only thing I can configure is IP and port.

For starters, I made the following server in Flask

from flask import Flask

app = Flask(__name__)


@app.route("/" ,methods=['GET', 'POST', 'DELETE', 'PATCH'])
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000)

I was getting a 308 error:

error 308

Then I used the following catch-all url approach to help my case.

from flask import Flask

app = Flask(__name__)


@app.route("/", defaults={"path": ""},methods=['GET', 'POST', 'DELETE', 'PATCH'])
@app.route("/<string:path>",methods=['GET', 'POST', 'DELETE', 'PATCH'])
@app.route("/<path:path>",methods=['GET', 'POST', 'DELETE', 'PATCH'])
def hello_world():
    return 'Hello World!'


if __name__ == '__main__':
    app.run(host='0.0.0.0',port=5000)

But still, I am getting the same error:

the error

4
  • here is the solution I found. check this out. Commented May 11, 2019 at 7:00
  • Tried it , I got 405 which meant method not allowed, I then added methods parameter and it was back to 308. Commented May 11, 2019 at 10:01
  • If you look at the second screenshot the request it not even coming at '/' it is coming at empty. normally there is a / if you send a request to that route Commented May 11, 2019 at 10:03
  • Does this answer your question? Why does my Flask app return a response with status code 308 under test? Commented Apr 28, 2021 at 11:38

2 Answers 2

4

Since I don't have 50 reputation yet, I can't comment so I'll leave this here.

You want to use the path variable for routes:

"A simple way to create a Catch-All function which serves every URL including / is to chain two route filters. One for the root path '/' and one including a path placeholder for the rest.

We can't just use one route filter including a path placeholder because each placeholder must at least catch one character."

from flask import Flask
app = Flask(__name__)

@app.route('/', defaults={'path': ''})
@app.route('/<path:path>')
def catch_all(path):
    return 'You want path: %s' % path

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

% curl 127.0.0.1:5000          # Matches the first rule
You want path:  
% curl 127.0.0.1:5000/foo/bar  # Matches the second rule
You want path: foo/bar

http://flask.pocoo.org/snippets/57/

Sign up to request clarification or add additional context in comments.

1 Comment

Tried it , I got 405 which meant method not allowed, I then added methods parameter and it was back to 308.
2

If you insist to use empty route, Just set app.route with strict_slashes=False, like this:

from flask import reqeust
@app.route('/', methods = ['GET','POST','DELETE', 'PATCH'], 
           strict_slashes=False)
def hello():
    return f"hello, {request.method}"

Because Flask use werkzeug.routing.Rule, which enable strict_slashes as default, visiting a branch URL without a trailing slash will redirect to the URL with a slash appended. Which cause response with 308 (Permanent Redirect).

refer: https://werkzeug.palletsprojects.com/en/1.0.x/routing/

1 Comment

Still applies to 2.2.x

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.