Content-Type

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.

Content-Type 表示标头用于指示资源的原始媒体类型(在发送时应用任何内容编码之前)。

在响应中,Content-Type 标头向客户端提供返回内容的实际内容类型。例如,当浏览器执行 MIME 嗅探时,该标头的值可能会被忽略;将 X-Content-Type-Options 标头值设置为 nosniff 可防止这种行为。

在请求(例如 POSTPUT)中,客户端会告诉服务器实际发送的数据类型。

标头类型 表示标头
禁止修改的标头
列入 CORS 白名单的响应标头
列入 CORS 白名单的请求标头 是,但额外限制了值不能包含 CORS 不安全的请求标头字节:0x00-0x1F(除了 0x09(HT))、"():<>?@[\]{} 和 0x7F(DEL)。
解析值(忽略参数)的 MIME 类型也必须是 application/x-www-form-urlencodedmultipart/form-datatext/plain

语法

http
Content-Type: text/html; charset=utf-8
Content-Type: multipart/form-data; boundary=something

指令

media-type

资源或数据的 MIME 类型

charset

字符编码标准。不区分大小写,推荐使用小写。

boundary

对于多部分实体,必须使用 boundary 指令。该指令由 1 至 70 个字符组成,这些字符选自一套已知能通过电子邮件网关的、非常健壮的字符集(并且不以空白字符结束)。它用于封装信息多个部分的边界。通常情况下,开头的边界前会加上两个破折号,而末尾边界的后面也会加上两个破折号。

示例

在 HTML 表单中使用 Content-Type

在 HTML 表单提交产生的 POST 请求中,请求的 Content-Type 属性由 <form> 元素上的 enctype 属性来指定。

html
<form action="/foo" method="post" enctype="multipart/form-data">
  <input type="text" name="description" value="一些文本" />
  <input type="file" name="myFile" />
  <button type="submit">提交</button>
</form>

请求内容如下(此处省略了不太感兴趣的标头):

http
POST /foo HTTP/1.1
Content-Length: 68137
Content-Type: multipart/form-data; boundary=---------------------------974767299852498929531610575

-----------------------------974767299852498929531610575
Content-Disposition: form-data; name="description"

一些文本
-----------------------------974767299852498929531610575
Content-Disposition: form-data; name="myFile"; filename="foo.txt"
Content-Type: text/plain

(上传文件 foo.txt 的内容)
-----------------------------974767299852498929531610575--

规范

Specification
HTTP Semantics
# status.206
HTTP Semantics
# field.content-type

浏览器兼容性

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-Type

Legend

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

Full support
Full support

参见