你見過 HTTP 哪些狀態(tài)碼?
- 101 Switch Protocol
- 200 Ok
- 201 Created
- 204 No Content
- 206 Partial Content
- 301 Moved Permanently
- 302 Found
- 304 Not Modified
- 307 Temporary Redirect
- 400 Bad Request
- 401 Unauthorized
- 403 Forbidden
- 404 Not Found
- 405 Method Not Allowed
- 413 Payload Too Large
- 418 I'm A Teapot
- 422 Unprocessable Entity
- 500 Internal Server Error
- 502 Bad Gateway
- 503 Service Unavailable
- 504 Gateway Timeout
101 Switch Protocol
升級協(xié)議,如從 http 到 ws
示例:
101
200 Ok
表示資源請求成功
示例:
- $ curl https://shanyue.tech --head
- HTTP/2 200
201 Created
資源創(chuàng)建成功,多用于 POST 請求
204 No Content
響應(yīng)不會返回 Body,一般由以下兩種情況
- 與 Options/Delete 請求搭配
- 打點類
示例一: 掘金為 Options 請求的狀態(tài)碼設(shè)置為 204
掘金為 Options 請求的狀態(tài)碼設(shè)置為 204
示例二: 知乎為 Delete 請求的狀態(tài)碼設(shè)置為 204,以下請求為取消關(guān)注
掘金為 Delete 請求的狀態(tài)碼設(shè)置為 204
示例三: 當(dāng)你在知乎看段子時,不妨打開控制臺,會發(fā)現(xiàn)一個是 204 的狀態(tài)碼
- curl 'https://www.zhihu.com/sc-profiler' \
- -H 'content-type: application/json' \
- --data-binary '[["i","production.heifetz.main.desktop.v1.Collector.screen.1536_960.count",1,1]]' \
- --compressed -vvv
- < HTTP/2 204
- < server: CLOUD ELB 1.0.0
- < date: Thu, 22 Oct 2020 07:46:30 GMT
- < content-type: text/html
- < vary: Accept-Encoding
- < x-backend-response: 0.002
- < cache-control: no-cache, no-store, must-revalidate, private, max-age=0
206 Partial Content
當(dāng)請求多媒體數(shù)據(jù)數(shù)據(jù)較大時,會進行分片傳輸。當(dāng)你在B站觀看視頻,打開開發(fā)者工具,會發(fā)現(xiàn)許多 206 狀態(tài)碼以及響應(yīng)頭 Content-Range
示例:
B站上的視頻資源請求狀態(tài)碼為206
301 Moved Permanently
永久重定向。http 轉(zhuǎn)向 https時,有時會使用 301,如 B 站。
- $ curl www.bilibili.com -vvv
- < HTTP/1.1 301 Moved Permanently
- < Server: Tengine
- < Date: Thu, 22 Oct 2020 08:04:59 GMT
- < Content-Type: text/html
- < Content-Length: 239
- < Connection: keep-alive
- < Location: https://www.bilibili.com/
302 Found
暫時重定向。http 轉(zhuǎn)向 https時,有時也會使用 302,如知乎
- $ curl www.zhihu.com -vvv
- < HTTP/1.1 302 Found
- < Location: https://www.zhihu.com/
- < Content-Length: 0
- < X-NWS-LOG-UUID: 16068764905156850032
- < Connection: keep-alive
- < Server: Lego Server
- < Date: Thu, 22 Oct 2020 08:20:29 GMT
- < X-Cache-Lookup: Return Directly
304 Not Modified
資源已被緩存,與之相關(guān)的響應(yīng)頭部有:
- ETag
- last-modified/if-modified-since
一般用作 index.html 等不帶 hash 的資源,由于示例太多,這里就不舉例了
307 Temporary Redirect
暫時重定向。也可作為 http 到 https 的重定向。還有一種用途用作 HSTS,當(dāng)谷歌瀏覽器發(fā)現(xiàn)某 http 資源已被加入到 HSTS 列表,瀏覽器內(nèi)部會通過 307 作重定向
示例:
山月個人博客網(wǎng)站的 HSTS
Stackoverflow 中的307
http 狀態(tài)碼中 301,302和307有什么區(qū)別
- 301,Moved Permanently。永久重定向,該操作比較危險,需要謹慎操作:如果設(shè)置了301,但是一段時間后又想取消,但是瀏覽器中已經(jīng)有了緩存,還是會重定向。
- 302,F(xiàn)ound。臨時重定向,但是會在重定向的時候改變 method: 把 POST 改成 GET,于是有了 307
- 307,Temporary Redirect。臨時重定向,在重定向時不會改變 method
400 Bad Request
可以用于做 API 的參數(shù)校驗
401 Unauthorized
當(dāng)沒有權(quán)限的用戶請求需要帶有權(quán)限的資源時,會返回 401,此時攜帶正確的權(quán)限憑證再試一次可以解決問題
知乎登錄失敗時
403 Forbidden
我就是不想讓你訪問,不管你的權(quán)限憑證是否正確!
In summary, a 401 Unauthorized response should be used for missing or bad authentication, and a 403 Forbidden response should be used afterwards, when the user is authenticated but isn’t authorized to perform the requested operation on the given resource.
- 403與401狀態(tài)碼有什么區(qū)別
404 Not Found
未找到資源
405 Method Not Allowed
我需要 POST 這條資源,你去 GET 個錘子
413 Payload Too Large
不要給我扔這么大的 Body,我處理不過來
418 I'm A Teapot
我是一個茶壺
我要拋咖啡,你卻扔給我一個茶壺?
也可以用來處理不合法的參數(shù)校驗,我想要個字符串,你給了我一個整數(shù)?
422 Unprocessable Entity
常用來處理不合法的參數(shù)校驗。
Github 上給某個項目點贊時,故意設(shè)置一個不正確的參數(shù)命名,會返回狀態(tài)碼 422
422
500 Internal Server Error
服務(wù)器內(nèi)部錯誤,很有可能是應(yīng)用層未捕獲錯誤而導(dǎo)致整個服務(wù)掛掉
502 Bad Gateway
網(wǎng)關(guān)超時,Nginx 上常見,從上游應(yīng)用層返回了響應(yīng)
503 Service Unavailable
由于大量流量造成服務(wù)忙,稍等一下說不定就能用了
504 Gateway Timeout
網(wǎng)關(guān)超時,上游應(yīng)用層遲遲未響應(yīng)
本文轉(zhuǎn)載自微信公眾號「全棧成長之路」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系全棧成長之路公眾號。