Is a URL allowed to contain a space?

后端 未结 10 1395
慢半拍i
慢半拍i 2020-11-22 04:02

Is a URI (specifically an HTTP URL) allowed to contain one or more space characters? If a URL must be encoded, is + just a commonly followed convention

10条回答
  •  醉梦人生
    2020-11-22 04:14

    Why does it have to be encoded? A request looks like this:

    GET /url HTTP/1.1
    (Ignoring headers)
    

    There are 3 fields separated by a white space. If you put a space in your url:

    GET /url end_url HTTP/1.1
    

    You know have 4 fields, the HTTP server will tell you it is an invalid request.

    GET /url%20end_url HTTP/1.1
    

    3 fields => valid

    Note: in the query string (after ?), a space is usually encoded as a +

    GET /url?var=foo+bar HTTP/1.1 
    

    rather than

    GET /url?var=foo%20bar HTTP/1.1 
    

提交回复
热议问题