zoukankan      html  css  js  c++  java
  • python接口自动化之request模块一些注意事项

    一:发送请求

    ------POST--------

    1、参数类型为:"Content-Type": "application/json",发送请求的时候,使用json来传递参数
    response = requests.post(url=url,json=json)
    2、参数类型为:Content-Type": "application/x-www-form-urlencoded,使用data来进行传递
    response = requests.post(url=url,data=data)

    3、文件上传接口的参数类型为:"Content-Type": "multipart/form-data;文件应该使用files这个参数来进行传递

     file = {"pic": ("bj01.png", open(r"C:UsersMuSenDesktoppageimageico.png", "rb"), "image/png")}

    response = requests.post(url=url, files=file,data=data)

    ------GET-------

    1、处理方式一:放到url地址后面
    url = "http://httpbin.org/get?name=musen&age=18"

    2、处理方式二:使用params来进行传递
    url1 = "http://httpbin.org/get"
    data = {
    "name":"musen",
    "age":18
    }
    response = requests.get(url=url1,params=data)
    print(response.text)

    二:打印返回内容
    1、text属性:自动识别内容的编码方式,有可能识别不准确出现乱码
    res1 = response.text
    print(res1,type(res1)) ---string 类型


    2、json方法:将字符串中的json类型数据转换为对应的python值
    使用json方法的前提:返回数据必须是json格式的
    res3 = response.json()
    print(res3, type(res3))  ----dict类型

    3、content属性
    res5 = response.content   #返回字节形式的数据
    res5 = response.content.decode("utf8")  ---string 类型   #指定utf-8格式进行编码,手动自动编码方式,对页面内容进行解码
    print(res5, type(res5))

    提取返回结果中的参数

    复制代码
    import jsonpath
    
    res = {'code': 0,
           'msg': 'OK',
           'data': {'id': 7800007,
                    'leave_amount': 0.0,
                    'mobile_phone': '18189098765',
                    'reg_name': '小柠檬',
                    'reg_time': '2020-03-21 09:49:27.0',
                    'type': 1,
                    'token_info': {'token_type': 'Bearer',
                                   'expires_in': '2020-03-21 11:16:59',
                                   'token': 'eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc4MDAwMDcsImV4cCI6MTU4NDc2MDYxOX0.-zjbWEbXF9qdfvW1Wn0640HZnv3Xkdrx0nDedRTcsgk_URgU185yA-e2SjQUvVfsjA-FpJSKSOF4jjB-Jyv47A'}},
           'data1': {'id': 7800007,
                     'leave_amount': 0.0,
                     'mobile_phone': '18189098765',
                     'reg_name': '小柠檬',
                     'reg_time': '2020-03-21 09:49:27.0',
                     'type': 1,
                     'token_info': {'token_type': 'Bearer',
                                    'expires_in': '2020-03-21 11:16:59',
                                    'token': 'eyJhbGciOiJIUzUxMiJ9.eyJtZW1iZXJfaWQiOjc4MDAwMDcsImV4cCI6MTU4NDc2MDYxOX0.-zjbWEbXF9qdfvW1Wn0640HZnv3Xkdrx0nDedRTcsgk_URgU185yA-e2SjQUvVfsjA-FpJSKSOF4jjB-Jyv47A'}},
                         }
    
    #  使用字典的键值对 提取方式去提取
    # # 提取用户的id
    member_id = res["data"]["id"]
    print(member_id)
    #
    # # 提取token值
    token = res["data"]["token_info"]["token"]
    print(token)
    
    
    # 使用jsonpath来提取
    member_id = jsonpath.jsonpath(res, "$.data..id")[0]
    print(member_id,type(member_id))
    
    token = jsonpath.jsonpath(res, "$.data1..token")[0]
    print(token)
    
    """
    . 代表直接子节点
    .. 代表子孙节点(不管层级)
    
    """

    复制代码

    拓展:

    文件下载

    复制代码
    res = requests.get(url="http://www.lemonban.com/images/upload/image/20190219/1550554131699.jpg")
    
    print(res.content)
    
    with open("lemon.png","wb") as f:
        f.write(res.content)
  • 相关阅读:
    .NET委托与事件文章收集
    WCF简介
    设计模式之单例模式
    设计模式之工厂模式
    设计模式之简单工厂模式
    Jquery中bind和live的区别
    C#性能优化实践
    蒋金楠How ASP.NET MVC Works?[持续更新中…]
    按指定质量保存图片
    .net 获取网站根目录总结
  • 原文地址:https://www.cnblogs.com/xiaoduanhe/p/12572312.html
Copyright © 2011-2022 走看看