request里的baseurl设置了没用
时间: 2023-12-27 18:05:14 浏览: 12
如果您使用requests库的Session对象进行HTTP请求,并且在Session对象中设置了baseurl属性,那么这个设置是有效的。这个属性指定了请求的基础URL,当使用相对路径时会自动拼接到baseurl后面。
例如:
```python
import requests
s = requests.Session()
s.baseurl = 'http://example.com/api/'
# 发送GET请求 http://example.com/api/users
response = s.get('users')
# 发送GET请求 http://example.com/api/users/1
response = s.get('users/1')
```
如果您使用requests库的函数进行HTTP请求,并且在函数中设置了baseurl参数,那么这个设置也是有效的。这个参数与Session对象的baseurl属性类似,指定了请求的基础URL。
例如:
```python
import requests
# 发送GET请求 http://example.com/api/users
response = requests.get('http://example.com/api/users', baseurl='http://example.com/api/')
# 发送GET请求 http://example.com/api/users/1
response = requests.get('users/1', baseurl='http://example.com/api/')
```
需要注意的是,如果使用了Session对象,那么Session对象中的baseurl属性会覆盖函数中的baseurl参数。如果同时设置了两者,那么会使用Session对象中的baseurl属性。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)