Python requests接口测试实现代码

1、get方法请求接口

url:显而易见,就是接口的地址url啦

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

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

举个例子:

1

2

3

4

5

6

7

8

import requests

url="http://api.shein.com/login"

header={"content-type":"application/x-www-form-urlencoded"}

param={"user_id":123456,"email":"123456@163.com"}

timeout=0.5

response = requests.get(url, headers=header, params=param, timeout=timeout)

# response = requests.request("get",url,headers=header,params=body,timeout=timeout)

print (response.text)

2、post方法请求接口

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

import requests

url="http://api.shein.com/login"

header={"content-type":"application/x-www-form-urlencoded"}

param={"user_id":123456,"email":"123456@163.com"}

timeout=0.5

response = requests.post(url, headers=header, data=param, timeout=timeout)

# response = requests.request("post",url,headers=header,data=param,timeout=timeout)

print (response.text)

1

2

3

4

5

6

7

8

import requests

url = "https://apipc.xinqgj.com/user/login"

payload = {"phone":"17779828887","pwd":"Ty+coun/mUj1saGV2OCK6p5kN9MNt8Uznj"}

headers = {'Content-Type': 'application/json'}

response = requests.request("POST", url, headers=headers, json = payload)

print(response.text)

3、requests.Session()请求接口

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

import requests

session = requests.Session()  #定义全局session,通过 session 保持会话

class Cms():

  def login(self):

    url = "http://192.168.1.110:8080/cms/manage/loginJump.do"

    header = {"Content-Type": "application/x-www-form-urlencoded"}

    parmas = {"userAccount": "admin", "loginPwd": "123456"}

    #通过全局 session 请求接口

    res = session.post(url=url, headers=header, data=parmas)

    print(res.json())

  def queryUserList(self):

    url = "http://192.168.1.110:8080/cms/manage/queryUserList.do"

    header = {"Content-Type": "application/x-www-form-urlencoded"}

    parmas = {"startCreateDate":"",

         "endCreateDate":"",

         "searchValue":"",

         "page":"1"}

    # 通过全局 session 请求接口

    res = session.post(url=url, headers=header, data=parmas)

    print(res.json())

if __name__ == '__main__':

  Cms().login()

  Cms().queryUserList()

注意:Python requests模块params、data、json的区别

  • requests 模块发送请求有 data、json、params 三种携带参数的方法。
  • params 在 get 请求中使用,data、json 在 post 请求中使用
    • 常见的 form 表单可以直接使用 data 参数进行报文提交,data 的对象则是 python 中的字典类型
    • 如果数据是 json 格式的参数,可直接使用 json 参数进行报文提交

4、接口的返回值操作

text:获取接口返回值的文本格式

json():获取接口返回值的json()格式

status_code:返回状态码(成功为:200)

headers:返回完整的响应头信息(headers['name']:返回指定的headers内容)

encoding:返回字符编码格式

url:返回接口的完整url地址

1

2

3

4

5

6

7

8

9

10

11

12

13

14

import requests

url = "https://xxxx.com/user/login"

payload = {"phone":"1777982xxxx","pwd":"Ty+coun/mUj1saGV2OCK6p5kN9MNt8UznjaGsQ5A/nKPSH1NZW"}

headers = {'Content-Type': 'application/json'}

response = requests.request("POST", url, headers=headers, json = payload)

print(response.text)

print(response.json())

print(response.status_code)

print(response.url)

print(response.headers)

print(response.encoding)

最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走! 希望能帮助到你!【100%无套路免费领取】

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值