天天看點

軟體測試 - 接口測試實戰 | GET/POST 請求差別詳解GET、POST 的差別總結示範環境搭建CURL 指令發起 GET/POST 請求GET/POST 請求對比

軟體測試 - 接口測試實戰 | GET/POST 請求差別詳解GET、POST 的差別總結示範環境搭建CURL 指令發起 GET/POST 請求GET/POST 請求對比

在日常的工作當中,HTTP 請求中使用最多的就是 GET 和 POST 這兩種請求方式。深度掌握這兩種請求方式的原理以及異同之處,也是之後做接口測試一個重要基礎。

GET、POST 的差別總結

  1. 請求行的 method 不同;
  2. POST 可以附加 body,可以支援 form、json、xml、binary等各種資料格式;
  3. 從行業通用規範的角度來說,無狀态變化的建議使用 GET 請求,資料的寫入與狀态建議用 POST 請求;

示範環境搭建

為了避免其他因素的幹擾,使用 Flask 編寫一個簡單的 Demo Server。

  1. 安裝flask
pip install flask           
  1. 建立一個 hello.py 檔案

    hello.py

from flask import Flask, request

app = Flask (_name__)

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route("/request", methods=['POST', 'GET']) 
def hellp():
    #拿到request參數 
    query = request.args 
    #El request form 
    post = request.form 
    #分别列印拿到的參數和form 
    return f"query: {query}\n"\
           f"post: {post}"           
  1. 啟動服務
export FLASK_APP=hello.py 
flask run           

提示下面資訊則表示搭建成功。

* Serving Flask app "hello.py" 
* Environment: production 
  WARNING: Do not use the development server in a production environment. Use a production WSGI server instead. 
* Debug mode: off 
* Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)           

CURL 指令發起 GET/POST 請求

發起 GET 請求,a、b參數放入 URL 中發送,并儲存在 get 檔案中:

curl 'http://127.0.0.1:5000/request?a=1&b=2' -V -S &>get           

發起 POST 請求,a、b參數以 form-data格式發送,并儲存在post 檔案中:

curl 'http://127.0.0.1:5000/request?' -d "a=1&b=2" -V -S &>post           

GET/POST 請求對比

注意:>的右邊為請求内容,<左邊為響應内容。

GET 請求過程

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> GET /request?a=1&b=2 HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.64.1
> Accept: */*
>
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 80
< Server: Werkzeug/0.14.1 Python/3.7.5
< Date: Wed, 01 Apr 2020 07:35:42 GMT
<
{ [80 bytes data]
* Closing connection 0
query: ImmutableMultiDict([('a', '1'), ('b', '2')])
post: ImmutableMultiDict([])           

POST 請求過程

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 5000 (#0)
> POST /request?a=1&b=2 HTTP/1.1
> Host: 127.0.0.1:5000
> User-Agent: curl/7.64.1
> Accept: */*
> Content-Length: 7
> Content-Type: application/x-www-form-urlencoded
>
} [7 bytes data]
* upload completely sent off: 7 out of 7 bytes
* HTTP 1.0, assume close after body
< HTTP/1.0 200 OK
< Content-Type: text/html; charset=utf-8
< Content-Length: 102
< Server: Werkzeug/0.14.1 Python/3.7.5
< Date: Wed, 01 Apr 2020 08:15:08 GMT
<
{ [102 bytes data]
* Closing connection 0
query: ImmutableMultiDict([('a', '1'), ('b', '2')])
post: ImmutableMultiDict([('c', '3'), ('d', '4')])           

對兩個檔案進行對比:

軟體測試 - 接口測試實戰 | GET/POST 請求差別詳解GET、POST 的差別總結示範環境搭建CURL 指令發起 GET/POST 請求GET/POST 請求對比

從圖中可以清楚看到 GET 請求的 method 為 GET,POST 請求的 method 為 POST,此外,GET 請求沒有 Content-Type 以及 Content-Length 這兩個字段,而請求行中的 URL 帶有 query 參數,是兩種請求都允許的格式。(End)

更多技術文章分享及測試資料點此擷取