转载来自:http://www.zzvips.com/article/102263.html

1、get方法请求接口

url:显而易见,就是接口的地址url啦
headers:请求头,例如:content-type = application/x-www-form-urlencoded
params:用于传递测试接口所要用的参数,这里我们用python中的字典形式(key:value)进行参数的传递。
举个例子:

  1. import requests
  2. url="http://api.shein.com/login"
  3. header={"content-type":"application/x-www-form-urlencoded"}
  4. param={"user_id":123456,"email":"123456@163.com"}
  5. timeout=0.5
  6. response = requests.geturl, headers=header, params=param, timeout=timeout
  7. # response = requests.request("get",url,headers=header,params=body,timeout=timeout)
  8. print (response.text)

2、post方法请求接口

  1. import requests
  2. url="http://api.shein.com/login"
  3. header={"content-type":"application/x-www-form-urlencoded"}
  4. param={"user_id":123456,"email":"123456@163.com"}
  5. timeout=0.5
  6. response = requests.posturl, headers=header, data=param, timeout=timeout
  7. # response = requests.request("post",url,headers=header,data=param,timeout=timeout)
  8. print (response.text)
  1. import requests
  2. url = "https://apipc.xinqgj.com/user/login"
  3. payload = {"phone":"17779828887","pwd":"Ty+coun/mUj1saGV2OCK6p5kN9MNt8Uznj"}
  4. headers = {'Content-Type': 'application/json'}
  5. response = requests.request("POST", url, headers=headers, json = payload)
  6. print(response.text)

3、requests.Session()请求接口

  1. import requests
  2. session = requests.Session() #定义全局session,通过 session 保持会话
  3. class Cms():
  4. def login(self):
  5. url = "http://192.168.1.110:8080/cms/manage/loginJump.do"
  6. header = {"Content-Type": "application/x-www-form-urlencoded"}
  7. parmas = {"userAccount": "admin", "loginPwd": "123456"}
  8. #通过全局 session 请求接口
  9. res = session.post(url=url, headers=header, data=parmas)
  10. print(res.json())
  11. def queryUserList(self):
  12. url = "http://192.168.1.110:8080/cms/manage/queryUserList.do"
  13. header = {"Content-Type": "application/x-www-form-urlencoded"}
  14. parmas = {"startCreateDate":"",
  15. "endCreateDate":"",
  16. "searchValue":"",
  17. "page":"1"}
  18. # 通过全局 session 请求接口
  19. res = session.post(url=url, headers=header, data=parmas)
  20. print(res.json())
  21. if __name__ == '__main__':
  22. Cms().login()
  23. 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. import requests
  2. url = "https://xxxx.com/user/login"
  3. payload = {"phone":"1777982xxxx","pwd":"Ty+coun/mUj1saGV2OCK6p5kN9MNt8UznjaGsQ5A/nKPSH1NZW"}
  4. headers = {'Content-Type': 'application/json'}
  5. response = requests.request("POST", url, headers=headers, json = payload)
  6. print(response.text)
  7. print(response.json())
  8. print(response.status_code)
  9. print(response.url)
  10. print(response.headers)
  11. print(response.encoding)