1. 程式人生 > >前端工程師必須要知道的HTTP部分

前端工程師必須要知道的HTTP部分

mov uri div muti permanent response 定向 not 錯誤

1. IETF組織制定的標準

rfc7234: https://tools.ietf.org/html/rfc7234 --- 原來的2616以被廢棄

2. 格式

HTTP分為 請求Request響應Response,如圖:

技術分享圖片

Request

Header:

技術分享圖片

Body:

一般請求體就是以下4種格式

  • application/json
  • application/x-www-form-urlencoded
  • mutipart/form-data
  • text/xml

Demo:

GET / HTTP/1.1
Host: time.geekbang.org

Response

Header:

技術分享圖片

Body:

一般響應體就是HTML、JSON 或者 二進制多媒體數據

Demo:

HTTP/1.1 301 Moved Permanently
Date: Fri, 25 Jan 2019 13:28:12 GMT
Content-Type: text/html
Content-Length: 182
Connection: keep-alive
Location: https://time.geekbang.org/
Strict-Transport-Security: max-age=15768000

<html>
<head><title>301 Moved Permanently</title></head
> <body bgcolor="white"> <center><h1>301 Moved Permanently</h1></center> <hr><center>openresty</center> </body> </html>

3. HTTP Method

  • GET (查)
  • POST (改)
  • PUT (增)
  • DELETE (刪)
  • HEAD (只返回響應頭的GET)
  • CONNECT (多用於WebSocket、HTTPS)
  • OPTIONs、TRACE (多用於調試)

4. HTTP Status Code

狀態代碼有以下:

  • 1xx:指示信息--表示請求已接收,繼續處理。
  • 2xx:成功--表示請求已被成功接收、理解、接受。
  • 3xx:重定向--要完成請求必須進行更進一步的操作。
  • 4xx:客戶端錯誤--請求有語法錯誤或請求無法實現。
  • 5xx:服務器端錯誤--服務器未能實現合法的請求。

常見狀態代碼、狀態描述的說明如下。

  • 200 OK:請求成功
  • 301 Moved Permanently 永久性重定向
  • 302 Move temporarily 臨時重定向
  • 304 Not Modified 客戶端緩存沒有更新
  • 400 Bad Request:客戶端請求有語法錯誤,不能被服務器所理解。
  • 401 Unauthorized:沒有身份認證
  • 403 Forbidden:沒有權限
  • 404 Not Found:沒有資源
  • 500 Internal Server Error:發生不可預期的錯誤
  • 503 Server Unavailable:暫時不能處理請求,一段時間後可能恢復正常

前端工程師必須要知道的HTTP部分