包含:
[
{
"description":"post_form请求和响应,模拟登录接口",
"request":
{
"headers":{"content-type":"application/x-www-form-unlencoded"},
"method":"post",
"uri":"/login",
"forms":{"username":"xiaoqiang","pws":"123123"}
}
"response":
{
"json":{"error_code":0,"reason":"successed","username":"xiaoqiang","checkstatus":"on"}
"status":200
}
},
{
...
}
]
Request是python中强大的请求库,基本所有的请求都是用它来完成
pip install requests
import requests
r=requests.get(url,params=None,headers=None,cookies=None,auth=None,timeout=o.oo1)
{'q': 'python', 'cat': '1001'}
'Content-Type':'application/x-www-form-urlencoded'
'Content-Type':'application/json'
# 如果是该格式,需要在请求header里加上
+ cookies
+ auth: 身份认证
+ timeout:请求超时时间,单位second
r=requests.post(url,data=none,headers=none,cookies=none,auth=none,timeout=0.01)
a = {'a': 1, 'b': 2}
b = {"a": 1, "b": 2}
stringA = str(a)
stringB = str(b)
jsA = json.dumps(a)
jsB = json.dumps(b)
print(stringA==stringB) #真,stringA={'a': 1, 'b': 2}
print(stringA==JSA) # 假,jsA={"a": 1, "b": 2}
## string和js的区别在于引号,json.loads()参数对象的字符串,除了要满足字典类型的格式外,所有的字符串对象必须是双引号。
data = {'key1':'value1','key2':'value2'} 字典类型
r = requests.post(url,data=data) #data须是字典类型
data = {'key1':'value1','key2':'value2'}
data = json.dumps(data)#将json格式转化为str类型
r = requests.post(url,data=eval(data))
`
headers = {"User-Agent":"test request headers"}
r = requests.post(url,headers=headers)
data = {
"sites": [
{ "name":"test" , "url":"www.test.com" },
{ "name":"google" , "url":"www.google.com" },
{ "name":"weibo" , "url":"www.weibo.com" }
]
}
r = requests.post(url,json=data)
files = {
'file':open('test.txt','rb')
}
r = requests.post(url,files=files)
with open( 'test.txt' ) as f:
r = requests.post(url,data = f)
若有其他put ,delete 接口,使用方法只要在import requests 的前提下把 get 或者post改成put即可,其余不
https://www.cnblogs.com/puresoul/p/7488700.html
name = re.findall(r'"name":(.+?),"age"',str(r.text))#返回的的lessonid是list类型,还要进行转化
lessonId=r.json()['lessonId']