Content-Type 标头
HTTP Content-Type 表示标头用于指示资源在应用任何内容编码之前的原始媒体类型。
在响应中,Content-Type 标头通知客户端返回数据的媒体类型。在诸如 POST 或 PUT 的请求中,客户端使用 Content-Type 标头指定发送到服务器的内容类型。如果服务器实现或配置对内容类型处理严格,则可能会返回 415 客户端错误响应。
Content-Type 标头与 Content-Encoding 不同,Content-Encoding 帮助接收方了解如何将数据解码为原始形式。
注意:如果浏览器对响应执行MIME 嗅探(或内容嗅探),则此值可能会被忽略。为了防止浏览器使用 MIME 嗅探,请将 X-Content-Type-Options 标头值设置为 nosniff。有关更多详细信息,请参阅MIME 类型验证。
| 头类型 | 表示形式头 |
|---|---|
| 禁止请求头 | 否 |
| CORS-safelisted 响应头 | 是 |
| CORS 安全列表请求头 | 是* |
* 值不能包含CORS 不安全的请求标头字节:"():<>?@[\]{},、删除 0x7F 以及控制字符 0x00 到 0x19(Tab 0x09 除外)。它还需要具有其解析值的媒体类型(忽略参数)为 application/x-www-form-urlencoded、multipart/form-data 或 text/plain。
语法
Content-Type: <media-type>
例如
Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=ExampleBoundaryString
指令
<media-type>-
资源或数据的媒体类型。可能包含以下参数
charset:指示使用的字符编码标准。该值不区分大小写,但首选小写。boundary:对于多部分实体,boundary参数是必需的。它用于划分消息多个部分的边界。该值由 1 到 70 个字符组成(不能以空格结尾),这些字符在不同系统(例如电子邮件网关)的上下文中已知是稳健的。通常,请求正文中的标头边界前面有两个破折号,最终边界末尾附加两个破折号。
示例
使用正确的内容类型提供资产
在以下两个示例响应中,JavaScript 和 CSS 资产分别使用 text/javascript 和 text/css 提供。这些资源的正确内容类型有助于浏览器更安全、更高效地处理它们。有关更多信息,请参阅正确配置服务器 MIME 类型。
HTTP/1.1 200
content-encoding: br
content-type: text/javascript; charset=utf-8
vary: Accept-Encoding
date: Fri, 21 Jun 2024 14:02:25 GMT
content-length: 2978
const videoPlayer=document.getElementById...
HTTP/3 200
server: nginx
date: Wed, 24 Jul 2024 16:53:02 GMT
content-type: text/css
vary: Accept-Encoding
content-encoding: br
.super-container{clear:both;max-width:100%}...
多部分表单中的 Content-Type
在 HTML 表单提交产生的 POST 请求中,请求的 Content-Type 由 <form> 元素的 enctype 属性指定。
<form action="/foo" method="post" enctype="multipart/form-data">
<input type="text" name="description" value="Description input value" />
<input type="file" name="myFile" />
<button type="submit">Submit</button>
</form>
请求看起来像以下示例,为简洁起见省略了一些标头。在请求中,为说明目的使用了 ExampleBoundaryString 边界,但实际上,浏览器会创建更像 ---------------------------1003363413119651595289485765 这样的字符串。
POST /foo HTTP/1.1
Content-Length: 68137
Content-Type: multipart/form-data; boundary=ExampleBoundaryString
--ExampleBoundaryString
Content-Disposition: form-data; name="description"
Description input value
--ExampleBoundaryString
Content-Disposition: form-data; name="myFile"; filename="foo.txt"
Content-Type: text/plain
[content of the file foo.txt chosen by the user]
--ExampleBoundaryString--
URL 编码表单提交中的 Content-Type
当表单不涉及文件上传且使用更简单的字段时,URL 编码表单可能更方便,其中表单数据包含在请求正文中
<form action="/submit" method="post">
<label for="comment">Comment:</label>
<input type="text" id="comment" name="comment" value="Hello!" />
<button type="submit">Submit</button>
</form>
POST /submit HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 15
comment=Hello!
使用 JSON 的 REST API 中的 Content-Type
许多 REST API 使用 application/json 作为内容类型,这对于机器到机器通信或程序化交互很方便。以下示例显示了一个 201 Created 响应,显示了成功请求的结果
HTTP/1.1 201 Created
Content-Type: application/json
{
"message": "New user created",
"user": {
"id": 123,
"firstName": "Paul",
"lastName": "Klee",
"email": "p.klee@example.com"
}
}
规范
| 规范 |
|---|
| HTTP 语义 # status.206 |
| HTTP 语义 # field.content-type |
浏览器兼容性
加载中…