import requests
url = 'http://127.0.0.1:8000/login'
data = {"username":"fei","password":12}
req = requests.post(url,data)
print(req.text)
>>>{"code": 200, "msg": "登录成功!"}
import requests
url = 'http://127.0.0.1:8000/login'
data = {"username": "fei", "password": 12}
req = requests.post(url, data)
print(req.json())#返回字典格式,前提是返回必须是json串
>>>{'msg': '登录成功!', 'code': 200}#返回的肯定是字典,都是无序的
#如何获取返回值里面的code
import requests
url = "http://127.0.0.1:8000/login"
data = {"username": "fei", "password": 12}
def get_code(url,data):#传入链接地址,和参数
res = requests.post(url,data).json()#获取了返回值
print(res)
return res['code']res的返回值是一个字典,获取字典里面的code就直接res【‘code’】
print(get_code(url, data))
>>>{'code': 200, 'msg': '登录成功!'}
>>>200#打印出返回值里面的['code']的值
r = requests.post('http://127.0.0.1:8000/add_stu',json=data2) #请求报文为json类型的
print(r.text)
#发带cookie的 header
sign = get_sign(url,data)
cookie = {'sign':sign}
header = {'touxinxi':'hahahah'}
data2 = {
"username":'william',
"real_name":"王绵绵",
"class":"巨蟹座",
"phone":"19312345674"
}
url = "http://127.0.0.1:8000/add_stu2"
r = requests.post(url,json=data2,cookies=cookie,headers=header) #header里面传的时候不能有中文
print(r.text)
#下面是上传文件的
file ={
'file_name':open('hahah.py',encoding='utf-8')
}
data = {"username":"fei",'pwd':12}
url="http://127.0.0.1:8000/upload"
r = requests.post(url,data,files=file)
print(r.text)