在做接口开发的时候,前后端的小伙伴们总会遇到需要http协议中的一个常见的属性,它就是Content-Type,出现在浏览器的请求和响应头中。接下来这篇文章我们来详细说明下Content-Type。
HTTP Content-Type 是 HTTP 协议中用来表示请求或响应中数据的媒体类型(也称为 MIME 类型)。它告诉客户端和服务器如何处理传输的数据。Content-Type 头字段通常出现在 HTTP 请求和响应的头部,用于指示数据的类型和编码方式。
以下是一些常见的 Content-Type 类型及其示例代码:
1. text/plain
表示纯文本内容。
Content-Type: text/plain; charset=utf-8
Hello, World!
2. text/html
表示 HTML 文档。
Content-Type: text/html; charset=utf-8
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
3. application/json
表示 JSON 格式的数据。
Content-Type: application/json; charset=utf-8
{
name: John,
age:
}
4. application/xml
表示 XML 格式的数据。
Content-Type: application/xml; charset=utf-8
<?xml version= encoding=UTF-8?>
<person>
<name>John</name>
<age></age>
</person>
5. application/x-www-form-urlencoded
表示 URL 编码的表单数据。
Content-Type: application/x-www-form-urlencoded; charset=utf-8
name=John&age=
6. multipart/form-data
表示多部分表单数据,通常用于文件上传。
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=name
John
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name=file; filename=example.txt
Content-Type: text/plain
This is the content of the file.
------WebKitFormBoundary7MA4YWxkTrZu0gW--
7. image/jpeg
表示 JPEG 图像。
Content-Type: image/jpeg
(binary data)
8. image/png
表示 PNG 图像。
Content-Type: image/png
(binary data)
9. application/octet-stream
表示二进制流数据,没有指定具体的子类型。
Content-Type: application/octet-stream
(binary data)
. audio/mpeg
表示 MPEG 音频。
Content-Type: audio/mpeg
(binary data)
. video/mp4
表示 MP4 视频。
Content-Type: video/mp4
(binary data)
. text/css
表示 CSS 样式表。
Content-Type: text/css; charset=utf-8
body {
background-color: lightblue;
}
. application/javascript
表示 JavaScript 脚本。
Content-Type: application/javascript; charset=utf-8
console.log(Hello, World!);
. application/pdf
表示 PDF 文档。
Content-Type: application/pdf
(binary data)
创作不易,如果这篇文章对你有用,欢迎点赞关注加评论哦。