用Python做接口自动化,写一个demo,要求有登录接口,从登录接口获取token,然后传给其他接口使用
时间: 2024-03-19 09:42:00 浏览: 21
好的,下面是一个简单的demo:
```
import requests
# 登录接口
def login():
url = 'https://example.com/login'
data = {'username': 'your_username', 'password': 'your_password'}
response = requests.post(url, data=data)
token = response.json()['token']
return token
# 其他接口
def other_api(token):
url = 'https://example.com/other_api'
headers = {'Authorization': 'Bearer ' + token}
response = requests.get(url, headers=headers)
# 处理其他接口返回结果的代码
if __name__ == '__main__':
token = login()
other_api(token)
```
在这个demo中,`login()` 函数模拟了登录接口的请求,从接口返回的结果中获取了 token,然后将 token 作为参数传给其他接口的请求头,以便其他接口能够通过认证。`other_api()` 函数模拟了其他接口的请求,其中的 `headers` 参数包含了 token,用于认证身份。
当你运行这个demo时,它会首先调用 `login()` 函数,然后将返回的 token 传递给 `other_api()` 函数,以便其他接口能够通过认证。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)