zoukankan      html  css  js  c++  java
  • 上有传参下传json的接口调用

    1、优化Myrequest

    import requests
    from conf.setting import log
    
    class MyRequest():
    	@staticmethod
    	def post(url,data=None,cookie=None,header=None,is_json=False,files=None,params=None):
    		data = data if data else {}
    		cookie = cookie if cookie else {}
    		header = header if header else {}
    		files = files if files else {}
    		try:
    			if is_json:
    				res = requests.post(url,json=data,cookies= cookie,headers = header,verify=False,files=files,params=params).json()
    			else:
    				res = requests.post(url, data=data, cookies=cookie, headers=header,verify=False,files=files,params=params).json()
    			log.debug('【接口返回数据:%s】'%res)
    			print('res...',res)
    		except Exception as e:
    			res = {'error':str(e)}  #如果接口调用出错的话,那么就返回一个有错误信息的字典
    			log.error('异常信息:接口调用失败! url 【%s】 data 【%s】 实际结果是 【%s】'%(url,data,res))
    		return res
    
    	@staticmethod
    	def get(url,data=None,cookie=None,header=None):
    		data = data if data else {}
    		cookie = cookie if cookie else {}
    		header = header if header else {}
    		try:
    			verify=False
    			res = requests.get(url, params=data, cookies=cookie, headers=header,verify=False).json()
    			log.debug('【接口返回数据:%s】'%res)
    			print('res...', res)
    		except Exception as e:
    			log.error('异常信息:接口调用失败! url 【%s】 data 【%s】'%(url,data))
    			res = {'error':str(e)}  #如果接口调用出错的话,那么就返回一个有错误信息的字典
    		return res
    

      

    2、上有传参下传json的接口调用

    import unittest,requests
    from lib.my_redis import my
    # from lib.my_sql import my_sql
    from conf.setting import BASE_URL
    from urllib.parse import urljoin
    from lib.my_request import MyRequest
    from lib.tools import login
    
    class Pt_XXX(unittest.TestCase):
        def test_c_XXXj(self):
            '''用例描述'''
            url = '/xxx/xxx/xxx/xxx'
            real_url = urljoin(BASE_URL, url)
            token = login()
            params = {'xx': xxx,
                      'xx': 'xxxx'
                      }
    
    
            data={
                "ts": xxx,
                "id": "XXX"
            }
    
            # res=requests.post(real_url,params=params,json=data).json()
            res = MyRequest.post(real_url, data,is_json=True,params=params)
            self.assertEqual(200, res.get('code'), msg='XXXX失败')
    
    if __name__ == '__main__':
        c=Pt_XXX()
        c.test_c_XXXj()
    View Code
  • 相关阅读:
    win10 1607 安装密钥 GVLK
    计算机意外地重新启动或遇到错误。windows安装无法继续。若要安装windows 请单击 确定 重新启动计算机
    在Mac中使用「dd」指令烧录ISO镜像文件到U盘
    一款免费好用的正则表达式工具:Regex Match Tracer
    全国移动短信信息中心号码查询大全
    VS2008 LINK : fatal error LNK1104: cannot open file 'atls.lib'错误解决方案
    理解全概率公式与贝叶斯公式
    改善程序员生活质量的 3+10 习惯
    TCP选项之SO_RCVBUF和SO_SNDBUF
    OpenSSL开发学习总结
  • 原文地址:https://www.cnblogs.com/jiadan/p/11896921.html
Copyright © 2011-2022 走看看