• 技术分享 | 实战详解接口测试请求方式Get、post


    1、请求方法不同

    2、post 可以附加 body,可以支持 form、json、xml、binary 等各种数据格式

    3、从行业通用规范的角度来说,如果对数据库不会产生数据变化的,比如查询操作,建议使用 GET 请求,数据的写入与状态建议用 POST 请求

    演示环境搭建

    为了避免其他因素的干扰,使用 flask 编写一个简单的 demo server。

    1、安装 flask

    1. pip install flask
    1. 创建一个 hello.py 文件
    1. from flask import Flask, request
    2. app = Flask(__name__)
    3. @app.route('/')
    4. def hello_world():
    5. return 'Hello, World!'
    6. @app.route("/request", methods=['POST', 'GET'])
    7. def hellp():
    8. #拿到request参数
    9. query = request.args
    10. #拿到request form
    11. post = request.form
    12. #分别打印拿到的参数和form
    13. return f"query: {query}\n"\
    14. f"post: {post}"
    1. 启动服务
    1. export FLASK_APP=hello.py
    2. flask run

    提示下面信息则表示搭建成功

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

    cURL命令发起GET、POST请求

    发起 GET 请求,a、b 参数放入 URL 中发送,并保存在 get 文件中

    1. curl 'http://127.0.0.1:5000/request?a=1&b=2' -v -s &>get

    发起 POST 请求,a、b 参数以 form-data 格式发送,并保存在 post 文件中

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

    GET、POST 请求对比

    注意:>的右边为请求内容,<右边为响应内容

    GET 请求过程

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

    POST 请求过程

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

    对两个文件进行对比:

    image1080×490 127 KB

    从图中可以清楚看到 GET 请求的 method 为 GET,POST 请求的 method 为 POST,此外,GET 请求没有 Content-Type 以及 Content-Length 这两个字段,而请求行中的 URL 带有 query 参数是两种请求都允许的格式。

     

  • 相关阅读:
    公钥加密算法-RSA
    PGL图学习之基于GNN模型新冠疫苗任务[系列九]
    python用内省优化显示
    win 安装 Xshell 5
    <C++>解密 构造函数和析构函数
    MacBookPro更新到13后vagrant无法启动virtualBox
    Centos7配置静态IP详细步骤
    语音合成(TTS)应用方案一二三
    2024中国人民大学计算机考研分析
    Redis 6.0多线程模型比单线程优化在哪里了
  • 原文地址:https://blog.csdn.net/ceshiren456/article/details/125886704