Is a trailing ampersand legal in a URL?

前端 未结 2 614
无人及你
无人及你 2020-12-20 11:35

A URL like

http://localhost/path?a=b&c=d

is fine - but what is the status of the same URL with a trailing ampersand?

ht         


        
相关标签:
2条回答
  • 2020-12-20 11:37

    The URI syntax spec is for generic URIs. It allows anything in the query. I am not aware of any specification which actually specifies ampersand-separated key=value pairs. I believe it is merely convention. I know that PHP, for example, offers an option for using a different separator. But now, everyone uses ampersand-separated things when they want key-value pairs. You still occasionally come across things which use it for just a simple string, e.g. http://example.com/?example. This is perfectly valid.

    The basic answer, though, is that & is valid anywhere in the query string, including at the end.


    Demistifying the RFC syntax, or Why & is valid anywhere in the query string:

    First, you have

    query       = *( pchar / "/" / "?" )
    

    (So a query string is made of any number of pchar and literal slashes and question marks.)

    Going back, you have

    pchar         = unreserved / pct-encoded / sub-delims / ":" / "@"
    

    And earlier still

    sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
                / "*" / "+" / "," / ";" / "="
    

    So a literal & is in sub-delims which is in pchar, so it's valid in query

    0 讨论(0)
  • 2020-12-20 11:42

    I think there is an unwritten rule that all RFCs must be near impossible to understand. You're not the first person unable to parse the grammar and - in my humble opinion - Spray also failed too.

    There is nothing wrong with a trailing ampersand. It is a legal character in the URI used to separate parameters. A trailing ampersand may be pointless, but it isn't invalid. Spray should (again, only in my opinion) be simply ignoring it.

    0 讨论(0)
提交回复
热议问题