Search code examples
pythonhtmlhttpstandardsurlencode

What is the standard for 'application/x-www-form-urlencoded' and an empty value?


A while ago, I had noticed that when encoding a map name: value to 'application/x-www-form-urlencoded, it renders something like that (here I use Python):

>>> from urllib import urlencode
>>> urlencode({'hello': '', 'blabla': 'hihi'})
'blabla=hihi&hello='

But the parsing (at least with Python), just strips the pairs that have an empty value :

>>> from urlparse import parse_qs
>>> parse_qs('blabla=hihi&hello=')
{'blabla': ['hihi']}

So ... is it the standard behaviour ? Where can I find the reference on how www-form-urlencoded should be parsed ? I have googled a while, found the RFCs for uris, W3c docs for forms, and so on but nothing about how the empty values should be treated. Can somebody give me a pointer to that ???


Solution

  • As far as I know, there is no "standard" for this. The only thing that is described (in the html spec, as you have found out), is how a browser should encode form data. What you want to do (or not) with empty values is up to you.

    Note that urlparse.parse_qs() has an optional parameter, keep_blank_values that allows you to control how it should handle those:

    >>> from urlparse import parse_qs
    >>> parse_qs('blabla=hihi&hello=', keep_blank_values=True)
    {'blabla': ['hihi'], 'hello': ['']}