51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 1839|回复: 1
打印 上一主题 下一主题

[转贴] 接口测试实战: GET/POST 请求区别详解

[复制链接]
  • TA的每日心情
    擦汗
    3 天前
  • 签到天数: 1027 天

    连续签到: 2 天

    [LV.10]测试总司令

    跳转到指定楼层
    1#
    发表于 2021-11-5 13:51:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    在日常的工作当中,HTTP 请求中使用最多的就是 GET 和 POST 这两种请求方式。深度掌握这两种请求方式的原理以及异同之处,也是之后做接口测试一个重要基础。
      GET、POST 的区别总结
      1、请求行的 method 不同;
      2、POST 可以附加 body,可以支持 form、json、xml、binary等各种数据格式;
      3、从行业通用规范的角度来说,无状态变化的建议使用 GET 请求,数据的写入与状态建议用 POST 请求。
      演示环境搭建
      为了避免其他因素的干扰,使用 Flask 编写一个简单的 Demo Server。
      1. 安装flask
    1.  pip install flask
    复制代码
    2. 创建一个 hello.py 文件
    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.       #El request form
    11.       post = request.form
    12.       #分别打印拿到的参数和form
    13.       return f"query: {query}\n"\
    14.              f"post: {post}"
    复制代码
    3. 启动服务
    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. Use a production WSGI server instead.
    4.   * Debug mode: off
    5.   * 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 请求过程
    *   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 参数,是两种请求都允许的格式。


    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有帐号?(注-册)加入51Testing

    x
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

  • TA的每日心情
    开心
    2021-6-9 14:08
  • 签到天数: 1 天

    连续签到: 1 天

    [LV.1]测试小兵

    2#
    发表于 2021-11-8 10:41:47 | 只看该作者
    面试问得挺多的,虽然这是一个比较基础的题目
    回复 支持 反对

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-10-3 11:21 , Processed in 0.080904 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表