1. 200 OK表示请求成功,也是使用最为广泛的状态码。
如果是 GET 请求,代表「资源获取成功」。
代码语言:javascript复制$ curl --head https://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform
Connection: keep-alive
Content-Length: 277
Content-Type: text/html
Date: Tue, 16 Aug 2022 03:30:21 GMT
Etag: "575e1f72-115"
Last-Modified: Mon, 13 Jun 2016 02:50:26 GMT
Pragma: no-cache
Server: bfe/1.0.8.18
2. 201 Created一般用以 POST 请求,代表服务器「资源创建成功」。
如在 Github 中,创建 Issue 成功,则返回一个 201 的状态码。
由于 POST 创建资源需要有认证信息,因此不在 Apifox Project1 中进行示例。
代码语言:javascript复制$ curl \
-X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token
https://api.github.com/repos/OWNER/REPO/issues \
-d '{"title":"Found a bug","body":"I'\''m having a problem with this.","assignees":["octocat"],"milestone":1,"labels":["bug"]}'
3. 204 No ContentNo Content,即服务器不会发送响应体(Response Body)。
它有以下场景:
PUT 请求,「修改资源的某个状态」,此时 204 代表修改成功,无需响应体。见 RFC7231之 204 状态码2
DELETE/OPTION 请求打点 API示例一: 掘金为 Options 请求的状态码设置为 204
示例二: 知乎为 Delete 请求的状态码设置为 204,以下请求代表取消关注某人。
由于 DELETE/OPTION 需要有认证信息,因此不在 Apifox Project1 中进行示例。
4. 206 Partial Content当客户端指定 Range 范围请求头时,服务器端将会返回部分资源,即 Partial Content,此时状态码为 206。
当请求音视频资源体积过大时,一般使用 206 较多。
如果你现在去哔哩哔哩随便去看几个视频,打开浏览器控制台网络面板,会发现诸多
与之相关的有以下 Header
range/content-range: 客户端发送 range 请求头指定范围,若满足范围,服务器返回响应头 content-range 以及状态码 206。若不满足,则返回 416 Range Not Satisfiable 状态码。代码语言:javascript复制range: bytes=1-20
content-range: bytes 1-20/33229
示例如下:
代码语言:javascript复制$ curl https://q.shanyue.tech -H 'Range: bytes=1-29' --include
HTTP/2 206
server: Tengine
content-type: text/html; charset=utf-8
content-length: 29
strict-transport-security: max-age=5184000
date: Tue, 16 Aug 2022 07:22:05 GMT
vary: Accept-Encoding
x-oss-request-id: 62FB459D0925293935AC120C
x-oss-cdn-auth: success
accept-ranges: bytes
etag: "CEE08C7240A168CDB08EBD34B48CB8F7"
last-modified: Mon, 15 Aug 2022 11:08:49 GMT
x-oss-object-type: Normal
x-oss-hash-crc64ecma: 6247728796594318031
x-oss-storage-class: Standard
cache-control: no-cache
content-md5: zuCMckChaM2wjr00tIy49w==
x-oss-server-time: 34
ali-swift-global-savetime: 1660634525
via: cache22.l2cn2635[55,54,200-0,M], cache50.l2cn2635[56,0], kunlun9.cn1593[92,92,206-0,M], kunlun1.cn1593[94,0]
x-cache: MISS TCP_MISS dirn:-2:-2
x-swift-savetime: Tue, 16 Aug 2022 07:22:05 GMT
x-swift-cachetime: 0
content-range: bytes 1-29/7887
timing-allow-origin: *
eagleid: 76b4381516606345255583297e
!DOCTYPE html>
5. 实例我在 Apifox3 中演示了知名网站关于 20x 状态码的使用场景。见文档4。
6. 作业201/204/206 状态码分别在什么情况下会出现?浏览个人常用网站,找到 201/204/206 状态码请求如何对某个资源进行范围请求,在 axios/fetch/request 等库中如何请求参考资料
[1]
Apifox Project:https://apifox.cn/a1shanyue
[2]
RFC7231之 204 状态码:https://httpwg.org/specs/rfc7231.html#status.204
