import requests
#1.get请求,查询所有学院信息
#请求地址
url01="http://127.0.0.1:8000/api/departments/"
#设置请求
res01=requests.request("get",url01)
#显示文本信息
print("第一种get请求方式",res01.text)
#默认
print("第一种get请求方式",res01)
#显示状态码
print("第一种get请求方式",res01.status_code)
#显示头部信息
print("第一种get请求方式",res01.headers)
print("_"*60)
#2.get请求,查询所有学院信息
url02="http://127.0.0.1:8000/api/departments/"
res02=requests.get(url02)
print("第二种请求方式",res02.text)
print("_"*60)
#3.get请求,带参数查询
url03="http://127.0.0.1:8000/api/departments/"
#设置参数
para03={"dep_name":"学院26"}
res03=requests.get(url=url03,params=para03)
print("带参数查询",res03.text)
print("_"*60)
#4.post请求01 纯文本格式
url04="http://127.0.0.1:8000/api/departments/"
data04='{"data":[{"dep_id":"python01","dep_name":"Test学院","master_name":"Test-Master","slogan":"HereisSlogan"}]}'
headers04={"Content-Type":"application/json"}
#设置编码格式
res04=requests.post(url04,data04.encode("utf-8"),headers=headers04)
print("post请求,纯文本格式",res04.text)
print("_"*60)
#5.post请求,json格式
url05="http://127.0.0.1:8000/api/departments/"
json05= \
{
"data": [
{
"dep_id":"T01",
"dep_name":"Test学院",
"master_name":"Test-Master",
"slogan":"Here is Slogan"
}
]
}
res05=requests.post(url05,json=json05)
print("post请求,json格式",res05.text)
print("_"*60)