python 接口自动化测试框架-requests库使用

本文详细介绍了Python中requests库的使用方法,包括GET和POST请求的发送过程,以及不同编码方式的应用,如application/x-www-form-urlencoded、application/json和multipart/form-data。通过实例展示了如何设置请求头和数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、requests库的安装
从pycharm->File->setting,安装requests库
在这里插入图片描述
二、requests发送post请求
①、post请求方式的编码有三种:
Ⅰ、application/x-www-form-urlencoded:最常用的post提交数据的方式,以form表单形式提交数据

import requests

url = 'http://www.oktest.org.cn/siteApp/command/ecGzSubSearch?fid=t_xmosta&siteId=2808&search=1'
data = {
    'searchKey':"测试",
    'x':'30',
    'y':'8'
}
headers = {
    'Content-Type':'application/x-www-form-urlencoded'
}
res = requests.post(url=url,data=data,headers=headers)
print(res)

Ⅱ、application/json:以json格式提交数据

import requests
import json

url = 'http://www.oktest.org.cn/siteApp/command/ecGzSubSearch?fid=t_xmosta&siteId=2808&search=1'
data = {
    'searchKey':"测试",
    'x':'30',
    'y':'8'
}
headers = {
    'Content-Type':'application/json'
}
res = requests.post(url=url,data=json.dumps(data),headers=headers)
print(res)

Ⅲ、multipart/form-data:一般使用来上传文件(较少用)
这里就不过多介绍了,有兴趣的同学可以自行百度了解
三、requests发送get请求
1、发送get请求

url = 'http://www.oktest.org.cn'
res = requests.get(url)
print(res)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小志老师

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值