本节我们要把登陆态安装到 请求体中,首先我们先解决一个bug,就是当选择不添加登陆态时候,login_res未被定义的问题:
改成如下即可,默认{}
首先是简单的form-data和x-www... 这俩种的数据结构一样,所以方法几乎一样:
payload作为最终的请求体,我们只需要便利登陆态字段即可添加,如果没有,就是{} 也就不会执行这个for了。
测试一下:
可以看到,成功插入进来了。
然后是raw_json:
测试效果如下:
目前这三种请求体我们都可以成功插入 登陆态提取字段了。
完整代码:
# 调试层发送请求 def Api_send(request): # 提取所有数据 api_id = request.GET['api_id'] ts_method = request.GET['ts_method'] ts_url = request.GET['ts_url'] ts_host = request.GET['ts_host'] ts_header = request.GET['ts_header'] api_name = request.GET['api_name'] ts_body_method = request.GET['ts_body_method'] ts_project_headers = request.GET['ts_project_headers'].split(',') ts_login = request.GET['ts_login'] if ts_login == 'yes': #说明要调用登陆态了 login_res = project_login_send_for_other(project_id=DB_apis.objects.filter(id=api_id)[0].project_id) else: login_res = {} # 处理域名host if ts_host[:4] == '全局域名': project_host_id = ts_host.split('-')[1] ts_host = DB_project_host.objects.filter(id=project_host_id)[0].host if ts_body_method == '返回体': api = DB_apis.objects.filter(id=api_id)[0] ts_body_method = api.last_body_method ts_api_body = api.last_api_body if ts_body_method in ['',None]: return HttpResponse('请先选择好请求体编码格式和请求体,再点击Send按钮发送请求!') else: ts_api_body = request.GET['ts_api_body'] api = DB_apis.objects.filter(id=api_id) api.update(last_body_method=ts_body_method,last_api_body=ts_api_body) # 发送请求获取返回值 if ts_header == '': ts_header = '{}' try: header = json.loads(ts_header) #处理header except: return HttpResponse('请求头不符合json格式!') for i in ts_project_headers: if i!= '': project_header = DB_project_header.objects.filter(id=i)[0] header[project_header.key] = project_header.value # 拼接完整url if ts_host[-1] == '/' and ts_url[0] =='/': #都有/ url = ts_host[:-1] + ts_url elif ts_host[-1] != '/' and ts_url[0] !='/': #都没有/ url = ts_host+ '/' + ts_url else: #肯定有一个有/ url = ts_host + ts_url # 插入登陆态字段 ## url插入 if '?' not in url: url += '?' for i in login_res.keys(): url += i+'='+login_res[i]+'&' else: #证明已经有参数了 for i in login_res.keys(): url += '&'+i+'='+login_res[i] ## header插入 header.update(login_res) try: if ts_body_method == 'none': response = requests.request(ts_method.upper(), url, headers=header, data={} ) elif ts_body_method == 'form-data': files = [] payload = {} for i in eval(ts_api_body): payload[i[0]] = i[1] for i in login_res.keys(): payload[i] = login_res[i] response = requests.request(ts_method.upper(), url, headers=header, data=payload, files=files ) elif ts_body_method == 'x-www-form-urlencoded': header['Content-Type'] = 'application/x-www-form-urlencoded' payload = {} for i in eval(ts_api_body): payload[i[0]] = i[1] for i in login_res.keys(): payload[i] = login_res[i] response = requests.request(ts_method.upper(), url, headers=header, data=payload ) elif ts_body_method == 'GraphQL': header['Content-Type'] = 'application/json' query = ts_api_body.split('*WQRF*')[0] graphql = ts_api_body.split('*WQRF*')[1] try: eval(graphql) except: graphql = '{}' payload = '{"query":"%s","variables":%s}' % (query, graphql) response = requests.request(ts_method.upper(), url, headers=header, data=payload ) else: #这时肯定是raw的五个子选项: if ts_body_method == 'Text': header['Content-Type'] = 'text/plain' if ts_body_method == 'JavaScript': header['Content-Type'] = 'text/plain' if ts_body_method == 'Json': ts_api_body = json.loads(ts_api_body) for i in login_res.keys(): ts_api_body[i] = login_res[i] ts_api_body = json.dumps(ts_api_body) header['Content-Type'] = 'text/plain' if ts_body_method == 'Html': header['Content-Type'] = 'text/plain' if ts_body_method == 'Xml': header['Content-Type'] = 'text/plain' response = requests.request(ts_method.upper(), url, headers=header, data=ts_api_body.encode('utf-8')) # 把返回值传递给前端页面 response.encoding = "utf-8" DB_host.objects.update_or_create(host=ts_host) return HttpResponse(response.text) except Exception as e: return HttpResponse(str(e))