URL encoding the space character: + or ?

后端 未结 4 2107
迷失自我
迷失自我 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:40

    A space may only be encoded to "+" in the "application/x-www-form-urlencoded" content-type key-value pairs query part of an URL. In my opinion, this is a MAY, not a MUST. In the rest of URLs, it is encoded as %20.

    In my opinion, it's better to always encode spaces as %20, not as "+", even in the query part of an URL, because it is the HTML specification (RFC-1866) that specified that space characters should be encoded as "+" in "application/x-www-form-urlencoded" content-type key-value pairs (see paragraph 8.2.1. subparagraph 1.)

    This way of encoding form data is also given in later HTML specifications. For example, look for relevant paragraphs about application/x-www-form-urlencoded in HTML 4.01 Specification, and so on.

    Here is a sample string in URL where the HTML specification allows encoding spaces as pluses: "http://example.com/over/there?name=foo+bar". So, only after "?", spaces can be replaced by pluses. In other cases, spaces should be encoded to %20. But since it's hard to correctly determine the context, it's the best practice to never encode spaces as "+".

    I would recommend to percent-encode all character except "unreserved" defined in RFC-3986, p.2.3

    unreserved = ALPHA / DIGIT / "-" / "." / "_" / "~"
    

    The implementation depends on the programming language that you chose.

    If your URL contains national characters, first encode them to UTF-8 and then percent-encode the result.

提交回复
热议问题