Content-Location header
HTTP Content-Location
表示头标示了返回数据的备用位置。它主要用于指示通过内容协商传输的资源的 URL。
Content-Location
头标与 Location
头标不同。当发生内容协商时,Content-Location
指示访问该资源的直接 URL,允许客户端将来绕过此资源的内容协商。另一方面,Location
指示 3XX
重定向的目标,或 201 Created
响应中新创建资源的 URL。
语法
http
Content-Location: <url>
指令
示例
从服务器请求不同格式的数据
假设一个网站的 API 可以返回 JSON、XML 或 CSV 格式的数据。如果特定文档的 URL 是 https://example.com/documents/foo
,则网站可以根据请求的 Accept
头标返回不同的 Content-Location
URL。
请求头 | 响应头 |
---|---|
Accept: application/json, text/json |
Content-Location: /documents/foo.json |
Accept: application/xml, text/xml |
Content-Location: /documents/foo.xml |
Accept: text/plain, text/* |
Content-Location: /documents/foo.txt |
这些 URL 只是示例——该网站可以根据其需要,使用任何 URL 模式来提供不同的文件类型,例如查询字符串参数:/documents/foo?format=json
、/documents/foo?format=xml
等等。
然后客户端可以记住 JSON 版本在该特定 URL 可用,下次请求该文档时可以跳过内容协商。
服务器还可以考虑其他内容协商头标,例如Accept-Language
。
指示交易结果的 URL
假设你有一个 <form>
用于向网站的其他用户汇款。
html
<form action="/send-payment" method="post">
<p>
<label>
Who do you want to send the money to?
<input type="text" name="recipient" />
</label>
</p>
<p>
<label>
How much?
<input type="number" name="amount" />
</label>
</p>
<button type="submit">Send Money</button>
</form>
提交表单后,网站会为该交易生成一张收据。服务器可以使用 Content-Location
来指示该收据的 URL,以便将来访问。
http
HTTP/1.1 200 OK
Content-Type: text/html; charset=utf-8
Content-Location: /my-receipts/38
<!doctype html>
(Lots of HTML…)
<p>You sent $38.00 to ExampleUser.</p>
(Lots more HTML…)
规范
规范 |
---|
HTTP 语义 # field.content-location |
浏览器兼容性
加载中…