def run_api(url, method, params=None, body=None, header=None, cookie=None): request = Request() if str(method).lower() == "get": res = request.get(url, params=params, headers=header, cookies=cookie) elif str(method).lower() == "post": res = request.post(url, params=params, json=body, headers=header, cookies=cookie) elif str(method).lower() == "put": res = request.put(url, json=body, params=params, headers=header, cookies=cookie) elif str(method).lower() == "delete": res = request.delete(url, params=params, headers=header, cookies=cookie) else: log.error("请求方法 method 错误:method写成了 %s" % method) raise return res
def run_api(url, method, params_type, header=None, cookie=None, params=None): """ 发送api请求 """ request = Request() if str(method).lower() == "get": if str(params_type).lower() == "query_string": r = request.get(url, headers=header, data=params, cookies=cookie) else: r = request.get(url, headers=header, cookies=cookie) elif str(method).lower() == "post": if str(params_type).lower() == "form_data": r = request.post(url, data=params, headers=header, cookies=cookie) elif str(params_type).lower() == "json": r = request.post(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "put": if str(params_type).lower() == "form_data": r = request.put(url, data=params, headers=header, cookies=cookie) elif str(params_type).lower() == "json": r = request.post(url, json=params, headers=header, cookies=cookie) elif str(method).lower() == "delete": if str(params_type).lower() == "form_data": r = request.post(url, data=params, headers=header, cookies=cookie) elif str(params_type).lower() == "json": r = request.post(url, json=params, headers=header, cookies=cookie) else: log.error("错误请求methods:", method) return r