URL encoding the space character: + or ?

后端 未结 4 2012
迷失自我
迷失自我 2020-11-22 02:00

When is a space in a URL encoded to +, and when is it encoded to %20?

4条回答
  •  爱一瞬间的悲伤
    2020-11-22 02:53

    I would recommend %20.

    Are you hard-coding them?

    This is not very consistent across languages, though. If I'm not mistaken, in PHP urlencode() treats spaces as + whereas Python's urlencode() treats them as %20.

    EDIT:

    It seems I'm mistaken. Python's urlencode() (at least in 2.7.2) uses quote_plus() instead of quote() and thus encodes spaces as "+". It seems also that the W3C recommendation is the "+" as per here: http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1

    And in fact, you can follow this interesting debate on Python's own issue tracker about what to use to encode spaces: http://bugs.python.org/issue13866.

    EDIT #2:

    I understand that the most common way of encoding " " is as "+", but just a note, it may be just me, but I find this a bit confusing:

    import urllib
    print(urllib.urlencode({' ' : '+ '})
    
    >>> '+=%2B+'
    

提交回复
热议问题