Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

werkzeug request.query_string is of type str rather than bytes #38

Closed
ltalirz opened this issue Sep 17, 2020 · 4 comments
Closed

werkzeug request.query_string is of type str rather than bytes #38

ltalirz opened this issue Sep 17, 2020 · 4 comments
Labels

Comments

@ltalirz
Copy link

ltalirz commented Sep 17, 2020

When using the flask request object (which comes from the werkzeug library), I find that using the tiangolo/meinheld-gunicorn-flask:python3.8 docker image the request.query_string attribute is of type str, while the werkzeug documentation clearly states that it should be of type bytes:

query_string
The URL parameters as raw bytestring.

This resulted in issues in migrating an existing application to the tiangolo/meinheld-gunicorn-flask:python3.8 docker image, since it tried to decode request.query_string (str objects have no .decode method).

I suspect this comes somehow from the combination with meinheld, but I open the issue here since this makes it easy to reproduce:

Minimum working example

Dockerfile

FROM tiangolo/meinheld-gunicorn-flask:python3.8

COPY main.py /app/main.py

main.py

from flask import Flask
from flask import request
app = Flask(__name__)

@app.route("/")
def hello():
    msg = " request.query_string is of type {}\n".format(type(request.query_string))

    return msg
docker build  -t query-string .
docker run -d -p 4321:80 query-string
curl localhost:4321
# returns:  request.query_string is of type <class 'str'>
@tiangolo
Copy link
Owner

That's quite interesting!

I see that it behaves differently depending on if there's a query string or not.

$ curl "localhost/?qwer=34"
 request.query_string is of type <class 'bytes'>

$ curl localhost           
 request.query_string is of type <class 'str'>

I tested it here and I was able to confirm that the development Flask server (flask run) always returns request.query_string is of type <class 'bytes'>.

And Gunicorn alone also always returns request.query_string is of type <class 'bytes'>.

I also confirmed with my other image tiangolo/uwsgi-nginx-flask, and it behaves as expected, always with: request.query_string is of type <class 'bytes'>.


I think it's specifically a Meinheld issue. I tested with Meinheld alone, with:

from flask import Flask
from flask import request
app = Flask(__name__)

@app.route("/")
def hello():
    msg = " request.query_string is of type {}\n".format(type(request.query_string))

    return msg


if __name__ == "__main__":
    from meinheld import server

    server.listen(("0.0.0.0", 80))
    server.run(app)

And running that directly as a Python script, it shows the strange behavior.

Given that, it's not really an issue with this image per se, but with Meinheld, you might want to report it there: https://github.com/mopemope/meinheld

@ltalirz
Copy link
Author

ltalirz commented Jan 18, 2021

Thanks for looking into this @tiangolo - I've opened mopemope/meinheld#119

@github-actions github-actions bot removed the answered label Jan 18, 2021
@tiangolo
Copy link
Owner

Thanks for reporting it there @ltalirz! 🚀

In that case, I think we can close this one, right?

Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.

@ltalirz
Copy link
Author

ltalirz commented Nov 10, 2022

Sure, keeping the other issue open is good enough

@ltalirz ltalirz closed this as completed Nov 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants