网站首页 > 技术文章 正文
在日常的工作当中,HTTP 请求中使用最多的就是 GET 和 POST 这两种请求方式。深度掌握这两种请求方式的原理以及异同之处,也是之后做接口测试一个重要基础。
GET、POST 的区别总结
- 请求行的 method 不同;
- POST 可以附加 body,可以支持 form、json、xml、binary等各种数据格式;
- 从行业通用规范的角度来说,无状态变化的建议使用 GET 请求,数据的写入与状态建议用 POST 请求;
演示环境搭建
为了避免其他因素的干扰,使用 Flask 编写一个简单的 Demo Server。
- 安装flask
pip install flask
- 创建一个 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}"
- 启动服务
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 参数,是两种请求都允许的格式。(End)
(文章来源于霍格沃兹测试学院)
猜你喜欢
- 2024-11-06 JavaScript学习笔记(二十五)——HTTP
- 2024-11-06 原生js实现文件下载并设置请求头header
- 2024-11-06 干货-Http请求get、post工具类(get和post请求的区别是什么)
- 2024-11-06 聊聊在springcloud gateway如何获取请求体
- 2024-11-06 python接口自动化-发送get请求(python get请求 url传参)
- 2024-11-06 想测试HTTP响应不知道如何开展怎么办?
- 2024-11-06 接口测试遇到500报错?别慌,你的头部可能有点问题
- 2024-11-06 一文讲清HPP的请求方法和过程(hp partsufer)
- 2024-11-06 HTTP请求对象(获取用户请求信息)(如何查看http请求的头部信息)
- 2024-11-06 学习笔记-HTTP 请求方法详解(学习笔记-HTTP 请求方法详解pdf)
- 1507℃桌面软件开发新体验!用 Blazor Hybrid 打造简洁高效的视频处理工具
- 506℃Dify工具使用全场景:dify-sandbox沙盒的原理(源码篇·第2期)
- 486℃MySQL service启动脚本浅析(r12笔记第59天)
- 466℃服务器异常重启,导致mysql启动失败,问题解决过程记录
- 464℃启用MySQL查询缓存(mysql8.0查询缓存)
- 444℃「赵强老师」MySQL的闪回(赵强iso是哪个大学毕业的)
- 423℃mysql服务怎么启动和关闭?(mysql服务怎么启动和关闭)
- 419℃MySQL server PID file could not be found!失败
- 最近发表
- 标签列表
-
- c++中::是什么意思 (83)
- 标签用于 (65)
- 主键只能有一个吗 (66)
- c#console.writeline不显示 (75)
- pythoncase语句 (81)
- es6includes (73)
- windowsscripthost (67)
- apt-getinstall-y (86)
- node_modules怎么生成 (76)
- chromepost (65)
- c++int转char (75)
- static函数和普通函数 (76)
- el-date-picker开始日期早于结束日期 (70)
- js判断是否是json字符串 (67)
- checkout-b (67)
- localstorage.removeitem (74)
- vector线程安全吗 (70)
- & (66)
- java (73)
- js数组插入 (83)
- linux删除一个文件夹 (65)
- mac安装java (72)
- eacces (67)
- 查看mysql是否启动 (70)
- 无效的列索引 (74)