文件上传:上传图片的类型是file,这里没有用到头部信息
import requests
def sendImg(img_path, img_name, img_type='image/jpeg'):
"""
:param img_path:图片的路径
:param img_name:图片的名称
:param img_type:图片的类型,这里写的是image/jpeg,也可以是png/jpg
"""
url = 'https://www.xxxxxxxxxx.com'
with open(img_path + img_name, "rb")as f_abs:
body = {
'camera_code': (None, "摄像头1"),
'image_face': (img_name, f_abs, img_type)
"time":(None, "2019-01-01 10:00:00")
}
response = requests.post(url=url, files=body).json
return response
if __name__=='__main__':
res = sendImg(img_path, img_name)
print(res)
**如果上传图片是数组时,value直接写图片路径就可以**
文件上传:上传的类型是file,用到头部信息
from urllib3 import encode_multipart_formdata
import requests
def sendFile(filename, file_path):
"""
:param filename:文件的名称
:param file_path:文件的绝对路径
"""
url = "https://www.xxxxxxx.com"
with open(file_path, mode="r", encoding="utf8")as f:
file = {
"file": (filename, f.read()),
"key": "value",
}
encode_data = encode_multipart_formdata(file)
file_data = encode_data[0]
headers_from_data = {
"Content-Type": encode_data[1],
"Authorization": token
}
response = requests.post(url=url, headers=headers_from_data, data=file_data).json()
return response
if __name__=='__main__':
res = sendFile(filename, file_path)
print(res)