第一,现有的钉钉接口文件上传说明示例太过简单
第二,自己过python的requesets post方式进行:
将access_token,type设置到params,将media 设置到files,最后通过requests.request(method=method,url=url, **kwargs )上传,能上传,不过文件没有后缀,二进制数据都直接被当做文本在网页呈现,能否说一下python的上传思路?
from urllib3 import encode_multipart_formdata
import requests
headers={}
data={}
url = 'https://oapi.dingtalk.com/media/upload?access_token=' + access_token + '&type=file'
def post_files(url,headers,data,filename,filepath):
"""
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
"""
data['media']= ('test.py', open(r'C:\Users\Administrator\Desktop\test.py', 'rb').read())
encode_data = encode_multipart_formdata(data)
data = encode_data[0]
headers['Content-Type'] = encode_data[1]
r = requests.post(url, headers=headers, data=data)
print(json.loads(r.text)["media_id"])
from urllib3 import encode_multipart_formdata
import requests
headers={}
data={}
url = 'https://oapi.dingtalk.com/media/upload?access_token=' + access_token + '&type=file'
def post_files(url,headers,data,filename,filepath):
data['media']= ('test.py', open(r'C:\Users\Administrator\Desktop\test.py', 'rb').read())
encode_data = encode_multipart_formdata(data)
data = encode_data[0]
headers['Content-Type'] = encode_data[1]
r = requests.post(url, headers=headers, data=data)
print(json.loads(r.text)["media_id"])
for filename in file_dict.keys():
files[filename] = open(file_dict[filename], mode='rb')
r = requests.post(url, data=data,files=files)
如果你说的是钉钉这个接口的话:https://open-doc.dingtalk.com/docs/doc.htm?spm=a219a.7629140.0.0.JknMf3&treeId=172&articleId=104971&docType=1
应该是用这种方式来上传,接口文档描述得确实不是很清晰,给的是java的代码示例,对于mutipart格式的接口来说,这个接口协议就是耍榴芒。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。