语言:python,js
问题:前后端对接
前端发送POST
Postman发送Post
前端代码
前端发送Post后台响应
我在/getResponse也写了一个get请求的返回值,将代码改作get可以正确得到返回值
代码片
.
# -- coding: utf-8 --
import requests
import json
class Webrequests():
def get(self,url,data,headers):
try:
r = requests.get(url,params=data,headers=headers)
r.encoding = 'utf-8'
json_r = r.json()
print("Test执行结果:",json_r)
return json_r
except BaseException as e:
print("请求失败!",str(e))
def post(self,url,data,headers):
try:
r = requests.post(url,data=data,headers=headers)
r.encoding = 'utf-8'
json_r = r.json()
print("Test执行结果:",json_r)
return json_r
except BaseException as e:
print("请求失败!",str(e))
def post_json(self,url,data,headers):
try:
data = json.dumps(data)
r = requests.post(url,data=data,headers=headers)
r.encoding = 'utf-8'
json_r = r.json()
print("Test执行结果:",json_r)
return json_r
except BaseException as e:
print("请求失败!",str(e))
def run_main(self, method,url,data,headers):
result = None
if method == 'post':
result = self.post(url,data,headers)
elif method == 'get':
result = self.get(url,data,headers)
elif method == 'post_json':
result = self.post_json(url, data, headers)
else:
print("method值错误!!!")
return result
外界调用的时候,直接调取run_main方法,传相关参数
第一章先写到这里吧,后期逐渐更新,给大家看下运行的run类,以及生成的报告