实际上,HTTP的body可以是二进制数据,也可以是文本。HTTP协议本身不对body内容的格式做限制,具体格式取决于Content-Type
头字段的定义。
文本数据:
当Content-Type头字段指定为文本类型时(如text/plain
、text/html
、application/json
等),HTTP body中的内容通常是文本数据。
例如:
Content-Type: text/plain
This is a plain text message.
Content-Type: application/json
{
"name": "John",
"age": 30
}
请求发送JSON
数据:
POST /api/data HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 48
{
"username": "john_doe",
"email": "john@example.com"
}
二进制数据:
当Content-Type头字段指定为二进制类型时(如application/octet-stream
、image/png
、application/pdf
等),HTTP body中的内容是二进制数据。
例如:
Content-Type: application/octet-stream
[Binary data not displayable]
Content-Type: image/png
[Binary image data]
请求发送PNG
图片:
POST /upload HTTP/1.1
Host: example.com
Content-Type: image/png
Content-Length: 1024
[Binary image data]
HTTP协议通过Content-Type头字段来告知接收方如何解析body的内容,因此可以携带各种格式的数据,包括但不限于:
最后给大家推荐一个LinuxC/C++高级架构系统教程的学习资源与课程,可以帮助你有方向、更细致地学习C/C++后端开发,具体内容请见 https://xxetb.xetslk.com/s/1o04uB