Python 语句:requests.get(url, params=None, **kwargs)
(参考:python接口自动化测试(二)-requests.get())
(参考:2.5. 使用python requests调用接口)
1. 功能:发送一个 GET 请求
2. 参数:
url --- URL for the new Requests object
params --- (optional) dictionary or bites to be sent in the query string for the Request
**kwargs --- optional arguments that Requests takes
3. 返回值:Response object
4. 返回类型:requests.Response
5. requests 常用的响应信息:
6. 几种常见用法:
(1) 不带参数的 GET
import requests
url = "http://0.0.0.0:1000/users" # entry task 的接口 1
r = requests.get(url)
可以单步调试看 response 的响应信息:
(2) 带 header 的 GET
url = "https://......" # 获取手机验证码的 url
headers = {"cid": "..", "env": "..", "number": ".."}
response = requests.get(url, headers)
result = json.loads(response.json()["data"]) # 字符串格式改为 dict 格式
verification_code = result["CodeData"]["Code"]
通过单步调试看运行结果,想要的结果在 response 的 text 里面,它是 string 类型,通过response.json() 转化成了字典格式。注意:需要手机发送验证码才能运行 requests 请求,否则无响应验证码。