一,以multipart形式发送post请求,只要将requests.post()的files参数即可
import requests,json
url = "http://httpbin.org/post"#一个测试网站的地址
files = {"file":open("test.txt","rb")}#在相对路径下找到test.txt的文件,给到files变量
re = requests.post(url,files=files)
print(re.text)
print(re.status_code)
返回数据,注意files中的数据,就是TXT文本中写入的内容,content类型是multipart/form-data
{
"args": {},
"data": "",
"files": {
"file": "Hello Python!!!!"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "160",
"Content-Type": "multipart/form-data; boundary=a93c21e8ba7c134b0a78a6bdc992e75b",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6018ec93-3376f0bf44a62fca34ac5163"
},
"json": null,
"origin": "202.127.0.116",
"url": "http://httpbin.org/post"
}
200
二,以json形式发送请求
import requests,json
url = "http://httpbin.org/post"#一个测试网站的地址
data={"key1":"value1",
"key2":"value2",
"key3":"value3"}
#用json里面的dumps方法将data数据转换成json数据
json_data = json.dumps(data)
re = requests.post(url,json_data)
print(re.text)
print(re.status_code)
返回值如下,json里有数据,jsons数据,这个为啥不显示content-type?
{
"args": {},
"data": "{\"key1\": \"value1\", \"key2\": \"value2\", \"key3\": \"value3\"}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "54",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6018ee99-6d0939483ed19072080f386d"
},
"json": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"origin": "202.127.0.116",
"url": "http://httpbin.org/post"
}
200
三,是最常用的方式,表单形式
import requests,json
url = "http://httpbin.org/post"#一个测试网站的地址
data={"key1":"value1",
"key2":"value2",
"key3":"value3"}
#这里直接将数据传入到请求里面
re = requests.post(url,data)
print(re.text)
print(re.status_code)
输出如下的内容:“Content-Type”: “application/x-www-form-urlencoded”,
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "35",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-6018f024-20df1180409fad4a7aebe307"
},
"json": null,
"origin": "202.127.0.116",
"url": "http://httpbin.org/post"
}
200
四,将上面的代码封装一下
1,
`import requests
#新建一个文件,命名为keyword_demo,将一些方法封装,等着其他文件调用
class KeyDemo:
#新建一个类 ,下面包含get请求个post请求
def get(self,url,header=None,params=None):
return requests.get(url=url,headers=header,params=params)
#传入参数,url外默认为none
def post(self,url,header=None,params=None):
return requests.post(url=url,headers=header,params=params)
2,在一个名为demo的文件中调用刚才封装的类
#导入刚才封装的方法,from文件名import类名
from keyword_demo import KeyDemo
#实例化一个对象,可以拿着个对象去调用keyword_demo里面的方法get和post
kd = KeyDemo()
url = "http://httpbin.org/post"#一个测试网站的地址
data={"key1":"value1",
"key2":"value2",
"key3":"value3"}
#这里拿着实例化对象kd调用类KeyDemo的方法
re = kd.post(url=url,params=data)
print(re.text)
print(re.status_code)
返回数据跟之前直接使用requests库一样的数据
{
"args": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-601900ed-4ce72146554fd82b71e664c9"
},
"json": null,
"origin": "202.127.0.116",
"url": "http://httpbin.org/post?key1=value1&key2=value2&key3=value3"
}
200
3,刚才执行了类的封装和调用,现在要将数据也封装进去。在同级目录下新建一个文件,命名为config.ini,内容为:
[DEFAULT]
url = http://httpbin.org/post
将此链接作为文件导入
4,调用刚才新建的ini文件,注意read后面的路径,同级目录下就不用加上’…/’,如果是不同目录,需要加上点点杠。
from keyword_demo import KeyDemo
#导入这个库
import configparser
#生成一个对象
conf = configparser.ConfigParser()
#用这个对象去读文件里面的信息
conf.read("config.ini")
#这句代码直接将链接打印出来,就是ini文件中的url
print(conf.get("DEFAULT","url"))
4,到这一步,将URL也封装了,现在将测试数据也封装一下,就是
data={“key1”:“value1”,
“key2”:“value2”,
“key3”:“value3”}
咋封装呢,新建一个文件,叫login.yaml,创建以下数据,不弄太多组数据,容易乱
data:
key1:111111111111,
(插入一下,这个yaml文件里的data不能有逗号,像这样也是可以的:
data:
key1:111111111111
key2:222222222222
key1:111111111111
key2:222222222222
key1:111111111111
key2:222222222222
key1:111111111111
key2:222222222222
key1:111111111111
key2:222222222222
key1:111111111111
key2:222222222222
key1:111111111111
key2:222222222222)
数据创建好了,我们要拿到这个数据,
from keyword_demo import KeyDemo
#导入yaml包
import yaml
#生成一个文件对象
file = open("login.yaml","r")
#用下面的命令将文件打开
data = yaml.load(file,yaml.FullLoader)
#输出一个字典:{'data': 'key1:111111111111'}
print(data)
5,现在为止,就已经把所有的数据封装完毕,汇总到demo文件里
#导入自己封装的类
from keyword_demo import KeyDemo
#导入类为了获得ini文件中的url,以后可以用的url的地方都可以调用这个
import configparser
#导入yaml为了获得data数据
import yaml
#生成一个实例对象
kd = KeyDemo()
#获取到url
conf = configparser.ConfigParser()
conf.read("config.ini")
url = conf.get("DEFAULT","url")
#获取到data
file = open("login.yaml","r")
data = yaml.load(file,yaml.FullLoader)
#创建一个请求对象
re = kd.post(url=url,params=data)
print(re.text)
print(re.status_code)
输出:
{
"args": {
"data": "key1:111111111111"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.24.0",
"X-Amzn-Trace-Id": "Root=1-60190d82-066258f37eccc1b429184705"
},
"json": null,
"origin": "202.127.0.116",
"url": "http://httpbin.org/post?data=key1%3A111111111111"
}
200
五,将pytest结合进来
1,将pytest加进来,新建测试类,测试cases
from keyword_demo import KeyDemo
import configparser
import yaml
#导入pytest
import pytest
#新建一个测试类
class Test_Demo02:
#新建一个用例
def test_api_01(self):
#生成一个实例对象
kd = KeyDemo()
#获取到url
conf = configparser.ConfigParser()
conf.read("config.ini")
url = conf.get("DEFAULT","url")
#获取到data
file = open("login.yaml","r")
data = yaml.load(file,yaml.FullLoader)
#创建一个请求对象
re = kd.post(url=url,params=data)
print(re.status_code)
assert 200 == re.status_code
2,生成报告,命令行里执行下面的代码,pytest <要执行的文件名> --html=<文件的名称.(格式,一般是html)>,会在当前文件夹下生成一个html的文件。
pytest test_demo02.py --html=report_test.html,如下图所示:
应该把断言加在关键字管理文件中,keywrod-demo.py 文件中,方便以后调用,暂时还没整明白