对于 REST API,URL 是否不区分大小写?

For REST APIs, are URLs meant to be case insensitive?

我正在阅读 W3C URL Specification,我注意到没有明确提及这一点。

实验

所以我在 curl 中尝试的是

www.google.com

然后

www.GOOGLE.com

和这些 return编辑了同一份文件。所以我想也许 google 拥有其域名的所有变体,所以我尝试了其他网站,但得到的结果好坏参半。

所以我在 URL 规范中混合了大小写,它似乎允许混合大小写。

将其应用于 REST API 设计

因此,当将其应用于 REST API 设计时,有时我们会使用标识符的概念来 return 来自服务器的特定资源。例如

https://localhost:8080/contacts/MYSELF中,MYSELF是典型的标识符

根据以往的经验,MYSELF的情况应该是无所谓的。但是如果我想对标识符进行严格验证怎么办?

当然,您可以违反规范并在应用程序中执行此操作;但是在这种情况下应该怎么做才合适呢?

言归正传。 URL是不区分大小写的吗?

域不区分大小写。您无需购买所有变体,因为获得域名即可获得所有变体。

没有规范说明 url 的 'path' 部分必须是特定情况。尽管路径 不区分大小写,因此访问同一域上的 /foo/FOO 可能 产生不同的资源。

根据 W3 规范 -

URLs in general are case-sensitive (with the exception of machine names). There may be URLs, or parts of URLs, where case doesn't matter, but identifying these may not be easy. Users should always consider that URLs are case-sensitive.

来源:- https://www.w3.org/TR/WD-html40-970708/htmlweb.html

通用 URI 语法

在通用的 URI 语法中(由 RFC 3986, which is currently the Internet Standard for URIs 定义),只有 两个部分不区分大小写:

  • Scheme:

    […] schemes are case-insensitive […]

  • Host:

    The host subcomponent is case-insensitive.

并且百分比编码三元组中的字母(即 a-fA-F)也不区分大小写。

其他所有内容都区分大小写。

然而, can overwrite this for their URIs (see Case Normalization).

HTTP(S) URI

在 HTTP(S) URI 的情况下,规范不会使任何其他组件不区分大小写(restates 方案和主机不区分大小写)。

这意味着以下 HTTP URI 等价:

http://example.com/foo
HTTP://example.com/foo
http://EXAMPLE.com/foo
http://example.COM/foo
HTTP://EXAMPLE.COM/foo
htTp://exAMPlE.cOm/foo

(最佳做法是将方案和主机规范化为小写。)

这些不等价:

http://example.com/foo?bar#baz
http://example.com/fOo?bar#baz
http://example.com/foo?bAr#baz
http://example.com/foo?bar#bAz
http://example.com/FOO?BAR#BAZ