这篇文章是写的如何访问一个需要登录的接口
接口文档中,需要传递的参数只有pageNo和pageSize,但是通过抓包可看见还需要传递sessionId
image.png
sessionId是有时效的,在登录后就会获取一个sessionId,所以这篇文章需要先导入一个能够获取sessionId的文件login_in
#这个是获取学院列表接口
import requests
import unittest
# 导入本地login_in.py文件
import login_in
sessionId = login_in.sessionId
# print("sessionId值为:"+sessionId)
body1 = {
"pageNo": 1,
"pageSize": 100,
"sessionId": sessionId
}
ip = 'http://www.*******.cn:8882/'
# 获取学院列表
interface = "/college/queryPageList"
url = ip + interface
head = {"Content-Type": "application/x-www-form-urlencoded"}
r = requests.post(url, data=body1, headers=head).text
print(r)
# 断言
def test(self):
self.assertIn("\"msg\":'成功'",r)
if __name__=='__main__':
unittest.main()
运行结果:
{"code":0,"msg":"成功!","data":{"offset":0,"limit":2147483647,"total":4,"size":100,"pages":1,"current":1,"searchCount":true,"openSort":true,"ascs":null,"descs":null,"orderByField":null,"records":[{"id":"233195780658561024","createTime":"2019-05-06 11:56:46","updateTime":"2019-06-24 11:13:33","logo":"/20190624/507ca37c-6de3-4549-b6fa-c8797526e452.png","name":"管理技术学院","status":1,"sort":0,"roleList":[{"id":"223859154366889984","createTime":"2019-04-10 17:36:21","updateTime":"2019-05-15 16:36:57","name":"系统管理员","type":"0","status":0,"menuList":[]},{"id":"223859156547928064","createTime":"2019-04-10 17:36:22","updateTime":"2019-05-14 15:05:04","name":"运营商","type":"0","status":0,"menuList":[]},{"id":"237240315340259328","createTime":"2019-05-17 15:48:18","updateTime":"2019-05-17 15:48:18","name":"安装工程师","type":"0","status":0,"menuList":[]}]},{"id":"233195780658561025","createTime":"2019-05-06 11:56:46","updateTime":"2019-06-24 11:00:02","logo":"/20190624/ee309a7c-b075-4ab8-bbaf-f0abdc14d71e.png","name":"门窗商学院","status":1,"sort":0,"roleList":[{"id":"223859616092651520","createTime":"2019-04-10 17:38:11","updateTime":"2019-05-14 15:05:34","name":"部门管理员","type":"0","status":0,"menuList":[]},{"id":"223859156547928064","createTime":"2019-04-10 17:36:22","updateTime":"2019-05-14 15:05:04","name":"运营商","type":"0","status":0,"menuList":[]}]},{"id":"236838038578528256","createTime":"2019-05-16 13:09:49","updateTime":"2019-05-30 17:33:00","logo":"/20190521/397424e9-a72c-4ecf-ae0a-f6492f961e73.jpg","name":"测试学院11","status":1,"sort":0,"roleList":[{"id":"223859154366889984","createTime":"2019-04-10 17:36:21","updateTime":"2019-05-15 16:36:57","name":"系统管理员","type":"0","status":0,"menuList":[]},{"id":"223859184515547136","createTime":"2019-04-10 17:36:28","updateTime":"2019-05-14 15:05:18","name":"普通员工","type":"0","status":0,"menuList":[]},{"id":"223859616092651520","createTime":"2019-04-10 17:38:11","updateTime":"2019-05-14 15:05:34","name":"部门管理员","type":"0","status":0,"menuList":[]}]},{"id":"238694304061980672","createTime":"2019-05-21 16:05:56","updateTime":"2019-05-30 17:33:02","logo":"/20190521/9d952cfa-c7fe-4ea1-83ea-1e8be51850cb.jpg","name":"新增学院","status":1,"sort":0,"roleList":[{"id":"223859154366889984","createTime":"2019-04-10 17:36:21","updateTime":"2019-05-15 16:36:57","name":"系统管理员","type":"0","status":0,"menuList":[]},{"id":"223859156547928064","createTime":"2019-04-10 17:36:22","updateTime":"2019-05-14 15:05:04","name":"运营商","type":"0","status":0,"menuList":[]},{"id":"223859184515547136","createTime":"2019-04-10 17:36:28","updateTime":"2019-05-14 15:05:18","name":"普通员工","type":"0","status":0,"menuList":[]},{"id":"223859616092651520","createTime":"2019-04-10 17:38:11","updateTime":"2019-05-14 15:05:34","name":"部门管理员","type":"0","status":0,"menuList":[]},{"id":"237239465314222080","createTime":"2019-05-17 15:44:56","updateTime":"2019-05-17 15:44:56","name":"销售工程师","type":"1","status":0,"menuList":[]},{"id":"237240315340259328","createTime":"2019-05-17 15:48:18","updateTime":"2019-05-17 15:48:18","name":"安装工程师","type":"0","status":0,"menuList":[]}]}],"condition":null,"asc":true}}
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
总结:
在写这篇文章的时候,犯了个错,引入的login_in文件返回的是正式服的sessionId,但是这篇文章是用的测试服的地址,导致报错“登录失效”
网友评论