Content-Location

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

HTTP Content-Location 表示标头指定返回数据的备用地址。最主要的用途是用来指定要访问的资源经过内容协商后的结果的 URL。

Content-Location 标头和 Location 标头是不同的。Content-Location 表示在内容协商发生时访问资源的直接 URL,允许客户端绕过该资源未来的内容协商。相比之下,Location 则表示 3XX 重定向的目标或者 201 Created 响应中新创建的资源的 URL。

标头类型 表示标头
禁止修改的标头

语法

http
Content-Location: <url>

指令

<url>

绝对地址,或者相对于请求 URL 的地址。

示例

请求数据的不同格式

假设,一个站点的 API 可以返回 JSONXMLCSV 格式。如果某个文件的 URL 是 https://example.com/documents/foo,站点可以根据请求的 Accept 标头返回不同的 Content-Location

请求标头 响应标头
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>
      你想把钱汇给谁?
      <input type="text" name="recipient" />
    </label>
  </p>

  <p>
    <label>
      多少金额?
      <input type="number" name="amount" />
    </label>
  </p>

  <button type="submit">汇款</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>
(很多 HTML……)

<p>你将 $38.00 汇给了示例用户。</p>

(很多 HTML……)

规范

Specification
HTTP Semantics
# field.content-location

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Content-Location

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见