在日常的工作当中,HTTP 请求中使用最多的就是 GET 和 POST 这两种请求方式。深度掌握这两种请求方式的原理以及异同之处,也是之后做接口测试一个重要基础。
GET、POST 的区别总结
请求行的 method 不同;POST 可以附加 body,可以支持 form、json、xml、binary等各种数据格式;从行业通用规范的角度来说,无状态变化的建议使用 GET 请求,数据的写入与状态建议用 POST 请求;演示环境搭建
为了避免其他因素的干扰,使用 Flask 编写一个简单的 Demo Server。
安装flask
pipinstallflask
创建一个 hello.py 文件
hello.py
fromflaskimportFlask,requestapp=Flask(_name__)@app.route('/')defhello_world():return'Hello,World!'@app.route("/request",methods=['POST','GET'])defhellp():#拿到request参数query=request.args#Elrequestformpost=request.form#分别打印拿到的参数和formreturnf"query:{query}"f"post:{post}"
启动服务
exportFLASK_APP=hello.pyflaskrun
提示下面信息则表示搭建成功。
*ServingFlaskapp"hello.py"*Environment:productionWARNING:Donotusethedevelopmentserverinaproductionenvironment.UseaproductionWSGIserverinstead.*Debugmode:off*Runningonhttp://127.0.0.1:5000/(PressCTRL+Ctoquit)
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 请求过程
*Trying127.0.0.1...*TCP_NODELAYset*Connectedto127.0.0.1(127.0.0.1)port5000(#0)>GET/request?a=1&b=2HTTP/1.1>Host:127.0.0.1:5000>User-Agent:curl/7.64.1>Accept:*/*>*HTTP1.0,assumecloseafterbody
POST 请求过程
*Trying127.0.0.1...*TCP_NODELAYset*Connectedto127.0.0.1(127.0.0.1)port5000(#0)>POST/request?a=1&b=2HTTP/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>}[7bytesdata]*uploadcompletelysentoff:7outof7bytes*HTTP1.0,assumecloseafterbody
对两个文件进行对比:
从图中可以清楚看到 GET 请求的 method 为 GET,POST 请求的 method 为 POST,此外,GET 请求没有 Content-Type 以及 Content-Length 这两个字段,而请求行中的 URL 带有 query 参数,是两种请求都允许的格式。(End)
(文章来源于霍格沃兹测试学院)
如果觉得《get request uni 参数_接口测试实战| GET/POST 请求区别详解》对你有帮助,请点赞、收藏,并留下你的观点哦!