內容位置

Content-Location 標頭指示返回資料的備用位置。主要用途是指示作為內容協商結果傳輸的資源的 URL。

LocationContent-Location 不同。Location 指示重定向的 URL,而 Content-Location 指示用於訪問資源的直接 URL,無需將來進一步進行內容協商。Location 是與響應關聯的標頭,而 Content-Location 與返回的資料關聯。在沒有示例的情況下,這種區別可能看起來很抽象。

標頭型別 表示標頭
禁止的標頭名稱

語法

http
Content-Location: <url>

指令

<url>

一個相對於請求 URL 的相對絕對 URL。

示例

以不同的格式從伺服器請求資料

假設某個站點的 API 可以以JSONXMLCSV 格式返回資料。如果某個特定文件的 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

指向新文件(HTTP 201 已建立)

假設您正在透過站點的 API 建立新的博文

http
POST /new/post
Host: example.com
Content-Type: text/markdown

# My first blog post!

I made this through `example.com`'s API. I hope it worked.

該站點在響應正文中返回已釋出的博文。伺服器使用 Content-Location 標頭指定新博文位於何處,表明此位置指的是此響應的內容(正文)

http
HTTP/1.1 201 Created
Content-Type: text/markdown
Content-Location: /my-first-blog-post

# My first blog post

I made this through `example.com`'s API. I hope it worked.

指示交易結果的 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

瀏覽器相容性

BCD 表格僅在啟用了 JavaScript 的瀏覽器中載入。

另請參閱