一、单字段发送单个文件
url = "http://httpbin.org/post"
data = None
files = { ... }
r = requests.post(url, data, files=files)
而这个files参数是可以接受很多种形式的数据,最基本的2种形式为字典类型和元祖列表类型
1、字典类型的files参数
{
"field1" : ("filename1", open("filePath1", "rb")),
"field2" : ("filename2", open("filePath2", "rb"), "image/jpeg"),
"field3" : ("filename3", open("filePath3", "rb"), "image/jpeg", {"refer" : "localhost"})
}
这个字典的key就是发送post请求时的字段名, 而字典的value则描述了准备发送的文件的信息;从上面可以看出value可以是2元组,3元组或4元组
缺省的话则会使用默认值。除了上面的使用形式,其实requests还是支持一个更简洁的参数形式,如下
{
"field1" : open("filePath1", "rb"),
"field2" : open("filePath2", "rb"),
"field3" : open("filePath3", "rb")
}
{
"field1" : ("filename1", open("filePath1", "rb")),
"field2" : ("filename2", open("filePath2", "rb")),
"field3" : ("filename3", open("filePath3", "rb"))
}
2、元祖列表类型的files参数
其实元组列表的形式与字典的形式基本一样,除了最外层的包装不一样;而在requests内部最终会把字典参数形式 转换 为 元组列的形式。官网推荐的用法如下:
[
("field1" : ("filename1", open("filePath1", "rb"))),
["field2" : ("filename2", open("filePath2", "rb"), "image/jpeg")],
("field3" : ("filename3", open("filePath3", "rb"), "image/jpeg", {"refer" : "localhost"}))
]
列表里面的子项可以是元组,也可以是列表;同样这里也支持简介的形式,如下:
[
("field1" : open("filePath1", "rb"))), ##filename 使用的是filepath的文件名
("field2" : open("filePath2", "rb").read())) ##filename 使用的是键值,即 field2
]
二、单字段发送多个文件,即上传文件时,设置为多选了
2.1、字典参数形式
{
"field1" : [
("filename1", open("filePath1", "rb")),
("filename2", open("filePath2", "rb"), "image/png"),
open("filePath3", "rb"),
open("filePath4", "rb").read()
]
}
2.2、元组列表形式
[
("field1" , ("filename1", open("filePath1", "rb"))),
("field1" , ("filename2", open("filePath2", "rb"), "image/png")),
("field1" , open("filePath3", "rb")),
("field1" , open("filePath4", "rb").read())
]
三、上传单张图片实例:
3.1、用fiddler抓包,查看接口请求格式和图片传参格式:
Content-Type:multipart/form-data
Content-Type: image/png
Content-Disposition: form-data; name="file"; filename="file.png"
3.2、上传图片
url='XXXXXXXXXXXXXX/1.0/bd/user/punch_clock'
files={"file": ("1.png", open(r"C:\Users\wangli\PycharmProjects\InterfaceAutomationTesting\case\1.png", "rb"), "image/png")}
headers={"Authorization":"bearereyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9vbnNpdGUtYXBpLnN0MS50ZXN0LmxhbnhpbmthLmNvbVwvMS4wXC9hZG1pblwvbWFuYWdlclwvbG9naW4iLCJpYXQiOjE1NTkxMjkzNjIsImV4cCI6MTU2MTgwNzc2MiwibmJmIjoxNTU5MTI5MzYyLCJqdGkiOiI4Y0VweGdnVDc3NjFwRkxnIiwic3ViIjoyMzUsInBydiI6IjA1NzdiNDFmNTRlMDk1YTNkOGNhMjM3NDM2NTAxYjhiN2IxZmU0YTQifQ.RUeRExypNq2sWY7CCOwH60xBEDgvRMytYKxEE2Ze35k"}
r=requests.post(url=url,headers=headers,files=files)
print(r.json())
四、上传图片实例:
4.1.上传图片以下这种格式:
-----------------------------22165374713946
Content-Disposition: form-data; name="localUrl"
yoyoketang.png
-----------------------------22165374713946
Content-Disposition: form-data; name="imgFile"; filename="yoyoketang.png"
Content-Type: image/png
2.写成字典格式,key对应name="imgFile"这里name对应的值
3.value里面是一个元组()
- 第一个参数是filename=后面对应的值,没有就写None
- 第二个是上面的这个值“”yoyoketang.png“”,或者是文件open打开的二进制流
- 第三个是Content-Type对应的类型,如:"image/png"
f ={
"localUrl": (None,"1.png"),
"imgFile": ("1.png", open("d:\1.png", "rb"), "image/png")
}
# 上传图片
url1 = "http://127.0.0.1:81/zentao/file-ajaxUpload-5a26aca290b59.html?dir=image"
f ={ "localUrl": (None,"1.png"), "imgFile": ("1.png", open("d:\\1.png", "rb"), "image/png") }
r = s.post(url1, files=f)
五、上传图片实例:
url='http://ta.2boss.cn/rabbit/v1/user/addAdvise'
data={"advise":"测试上传图片","customerId":"47066750",
"images":["http:\/\/2boss.oss-cn-hangzhou.aliyuncs.com\/54a0e078e4f4588d4a6efdfafa8cc621.jpg",
"http:\/\/2boss.oss-cn-hangzhou.aliyuncs.com\/82f7ad533662ca918f75035b8bc063f6.jpg"]}
headers={"TBSAccessToken":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJjbGllbnRJZCI6ImY3NDA4MWUzLTVhYzgtNDRkNS05NWFlLWM1OGM0ZmRhOWEyNCIsInNjb3BlIjoiQ09OU1VNRVIiLCJpc3MiOiJodHRwOi8vbGl5b3UuY28iLCJleHAiOjE1NjE3Nzg0MjcsInVzZXJJZCI6OTUxNTMsImV4cGlyZXNBdCI6MTU2MTc3ODQyNzcyNX0.4ZKIhwgQTTqxabsLs9_wX5r-dkaULgqfA2ENhkSnZPo"}
r=requests.post(url=url,headers=headers,json=data)
print(r.json())
六、传多个附件
1.传多个文件的时候如下,这两个参数的name都是一样的,如果用字典去传key值,很显然python的key值是不能重复的
Content-Disposition: form-data; name="files[]"; filename="1.png"
Content-Type: image/png
Content-Disposition: form-data; name="files[]"; filename="2.png"
Content-Type: image/png
2.这时候需要换个格式,传list数据
# file = {
# "files[]": ("1.png", open("d:\\1.png", "rb"), "image/png"),
# "labels[]": "tu1",
# }
#-------------多个文件用list类型------------
file = [
("files[]", ("2.png", open("d:\\1.png", "rb"), "image/png")),
("labels[]", "tu1"),
("files[]", ("2.png", open("d:\\2.png", "rb"), "image/png")),
("labels[]", "tu2"),
]