Flask: request.url returns decoded version. How to get encoded one?

Created on 2 Feb 2015  路  4Comments  路  Source: pallets/flask

If I have an URL 'http://localhost/test?p=%23iphone&q=http%3A%2F%2Fwww.google.com', when I use request.url it returns a decoded version:

In [9]: 'http://localhost/test?p=#iphone&q=http://www.google.com'

The problem is I cannot use urllib.parse on the decoded version, as it will return wrong result:

In [9]: urlparse('http://localhost/test?p=#iphone&q=http://www.google.com')
Out[9]: ParseResult(scheme='http', netloc='localhost', path='/test', params='', query='p=', fragment='iphone&q=http://www.google.com')

I cannot encode it back to correct version too:

In [11]: quote_plus('http://localhost/test?p=#iphone&q=http://www.google.com')
Out[11]: 'http%3A%2F%2Flocalhost%2Ftest%3Fp%3D%23iphone%26q%3Dhttp%3A%2F%2Fwww.google.com'
In [13]: quote('http://localhost/test?p=#iphone&q=http://www.google.com')
Out[13]: 'http%3A//localhost/test%3Fp%3D%23iphone%26q%3Dhttp%3A//www.google.com'

How can I get the correct encoded URL? I think I can use request.args, but it there a better way? I also want to include possible params and fragment, are there any information from request?

Most helpful comment

Same problem here. If one of my url parameters contains a quoted plus sign (%2B), is being decoded by flask as "+" and later when I use unquote_plus is being interpreted as a blank space. So, if my parameter contains both "%2B" and "+" they all end up being blank spaces and that's incorrect.

All 4 comments

It's impossible to get the original URL due to WSGI limitations.

Query arguments are available via request.args. Fragments aren't send to the server.

Why do you _need_ the URL?

Same problem here. If one of my url parameters contains a quoted plus sign (%2B), is being decoded by flask as "+" and later when I use unquote_plus is being interpreted as a blank space. So, if my parameter contains both "%2B" and "+" they all end up being blank spaces and that's incorrect.

I've got the same issue as eramirem, plusses get converted to spaces!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidism picture davidism  路  3Comments

ghostbod99 picture ghostbod99  路  4Comments

ghost picture ghost  路  3Comments

jab picture jab  路  4Comments

tomjaguarpaw picture tomjaguarpaw  路  3Comments