文章目录 1.request 1.1 request介绍 1.2 发送get请求 1.3 发送set请求 1.4 其
概念
请求方法的返回值response为 Response 对象,可以从这个对象中获取响应信息 比如: response.text
可以获取响应的文本内容
import requests# 发送GET请求response = requests.get("http://www.baidu.com")print("原始数据编码:",response.encoding)print("设置前响应数据:",response.text)#设置响应编码格式response.encoding = 'utf-8'print("数据编码:",response.encoding)print("设置后响应数据:",response.text)
说明
data: 参数接收fORM表单数据,后台会⾃动附加form表单请求信息头(data数据格式为字典)
JSON:参数接收json数据,后台会⾃动附加json表单请求信息头 (headers =
{“Content-Type”:“application/json”})
import requests#格式#response = requests.post(url,data=None,json=None)url = "http://localhost/index.PHP?m=Home&c=User&a=do_login"data1 = { "username":'"12345678911"',"passWord":"123456","verify_code":"8888"}response = requests.post(url,data=data1)print(response.json())
put:修改资源
delete:删除资源
head:响应数据中只包含请求头,前提是项目中已实现该功能
options:查看接口支持的请求方法,前提是项目中已实现该功能
import requestsresponse = requests.put("http://www.baidu.com", data={ "key": "value"})response = requests.delete("http://www.baidu.com")response = requests.head("http://www.baidu.com")response = requests.options("http://www.baidu.com")
概念
如果需要在URL的查询字符串中传递数据,可以使用params参数来定义,params传递的参数可以是字符串或字典
response = requests.get(url, params=None)response = requests.post(url, params=None, data=None)
案例:tpshop中搜索苹果
import requests#方法1:直接定义在urlresponse = requests.get("http://lo
来源地址:https://blog.csdn.net/meini32/article/details/129323524
--结束END--
本文标题: 软件测试-接口测试-代码实现接口测试
本文链接: http://www.lsjlt.com/news/396534.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0