python+requests接口自动化入门第一章(包括get 方法 post方法 及返回值的处理。)

本章主要讲接口自动化入门,包括get 方法 post方法 及返回值的处理。

 接口测试中见到最多的就是get方法和post方法。

get方法:

对于requests提供的get方法,有几个常用的参数:

url:接口的地址url

headers:定制请求头(headers),例如:content-type = application/x-www-form-urlencoded

params:用于传递测试接口所要用的参数,这里我们用python中的字典形式(key:value)进行参数的传递。

timeout:设置接口连接的最大时间(超过该时间会抛出超时错误)

url=‘http://api.shein.com/v2/member/logout’
header={‘content-type’: application/x-www-form-urlencoded}
param={‘user_id’: 123456,‘email’: [email protected]}
timeout=0.5
requests.get(url, headers=header, params=param, timeout=timeout)
url=‘http://api.shein.com/v2/member/logout’
header={‘content-type’: application/x-www-form-urlencoded}
param={‘user_id’: 123456,‘email’: 123456@163.com}
timeout=0.5
requests.get(url, headers=header, params=param, timeout=timeout)

POST方法

        与get方法类似,只要设置好对应的参数,就可以了。下面就直接举个栗子,直接上代码吧:

url=‘http://api.shein.com/v2/member/login’
header={‘content-type’: application/x-www-form-urlencoded}
data={‘email’: [email protected],‘password’: 123456}
timeout=0.5
requests.post(url, headers=header, data=data, timeout=timeout)
url=‘http://api.shein.com/v2/member/login’
header={‘content-type’: application/x-www-form-urlencoded}
data={‘email’: 123456@163.com,‘password’: 123456}
timeout=0.5
requests.post(url, headers=header, data=data, timeout=timeout)




猜你喜欢

转载自blog.csdn.net/qq_39208536/article/details/80214509