[Python-ideas] Proposed addtion to urllib.parse in 3.1 (and urlparse in 2.7)

Mart Sõmermaa mrts.pydev at gmail.com
Fri Mar 27 16:26:52 CET 2009

Appending query parameters to a URL is a very common need. However, there's
nothing in urllib.parse (and older urlparse) that caters for that need.

Therefore, I propose adding the following to 2.7 and 3.1 in the respective

def add_query_params(url, **params):
    Adds additional query parameters to the given url, preserving original

    >>> add_query_params('http://foo.com', a='b')
    >>> add_query_params('http://foo.com?a=b', b='c', d='q')

    The real implementation should be more strict, e.g. raise on the
    >>> add_query_params('http://foo.com?a=b', a='b')
    if not params:
        return url
    encoded = urllib.urlencode(params)
    url = urlparse.urlparse(url)
    return urlparse.urlunparse((url.scheme, url.netloc, url.path,
        (encoded if not url.query else url.query + '&' + encoded),
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090327/07dde551/attachment.html>

More information about the Python-ideas mailing list