当前位置:网站首页>技术分享 | 实战详解接口测试请求方式Get、post
技术分享 | 实战详解接口测试请求方式Get、post
2022-07-28 05:28:00 【叶赫那拉 赫敏】
1、请求方法不同
2、post 可以附加 body,可以支持 form、json、xml、binary 等各种数据格式
3、从行业通用规范的角度来说,如果对数据库不会产生数据变化的,比如查询操作,建议使用 GET 请求,数据的写入与状态建议用 POST 请求
演示环境搭建
为了避免其他因素的干扰,使用 flask 编写一个简单的 demo server。
1、安装 flask
pip install flask
- 创建一个 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
#拿到request form
post = request.form
#分别打印拿到的参数和form
return f"query: {query}\n"\
f"post: {post}"
- 启动服务
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 请求的 method 为 GET,POST 请求的 method 为 POST,此外,GET 请求没有 Content-Type 以及 Content-Length 这两个字段,而请求行中的 URL 带有 query 参数是两种请求都允许的格式。
边栏推荐
猜你喜欢

网络——传输层(详细版)

图形管线基础(一)

Bug experience related to IAP jump of stm32

mysql-8.0.17-winx64(附加navicat)手动配置版安装

Rain Scene Effect (I)

Project compilation nosuch*** error problem

Optimization ideas from ordinary query commodities to highly concurrent query commodities
![[dynamic planning -- the best period for buying and selling stocks series 3]](/img/9f/f6c07264f5ffaa0fdfcc724c713e83.png)
[dynamic planning -- the best period for buying and selling stocks series 3]

战疫杯--奇奇怪怪的形状

Yapi vulnerability hanging horse program chongfu.sh processing
随机推荐
中国剩余定理 个人理解
Feignclient @requestmapping parameter setting and simple method setting of request header
New Selenium
RayMarching实现体积光渲染
Leetcode skimming diary sword finger offer II 050. sum of downward path nodes
图形管线基础(二)
SSAO By Computer Shader(二)
Initializingbean interface and examples
OJ 1284 counting problem
RayMarching realizes volume light rendering
技术分享 | 接口自动化测试中,如何做断言验证?
Redis cache design and performance optimization
OJ 1129 fraction matrix
JS逆向100题——第1题
[pta ---- traversal of tree]
OJ 1131 beautiful number
js 变量等于0也等也' '问题
Graphic pipeline foundation (part outside)
Brief analysis of order transaction
项目编译NoSuch***Error问题