我正在尝试将文件发布到EchoSign API中,除了python请求之外,它对我来说无处不在。
我这里有完美的CURL命令
curl -H "Access-Token: API_KEY" \
-F File=@/home/user/Desktop/test123.pdf \
https://secure.echosign.com/api/rest/v2/transientDocuments
这是我的请求功能。它将上传PDF文件,但有垃圾,而CURL工作完美。
api_url = 'https://secure.echosign.com/api/rest/v2'
def send_document(file_path, access_token=access_token):
"""Uploads document to EchoSign and returns its ID
:param access_token: EchoSign Access Token
:param file_path: Absolute or relative path to File
:return string: Document ID
"""
headers = {'Access-Token': access_token}
url = api_url + '/transientDocuments'
with open(file_path, 'rb') as f:
files = {
'File': f,
}
return requests.post(url, headers=headers, files=files).json().get('transientDocumentId')
我做错了什么?我已经尝试将文件作为数据而不是文件发布,但仍然没有不同的结果
由于
修改
我添加
时有效data = {
'Mime-Type': 'application/pdf',
'File-Name': 'abc.pdf'
}
所以,我的新功能是:
def send_document(file_path, access_token=access_token):
"""Uploads document to EchoSign and returns its ID
:param access_token: EchoSign Access Token
:param file_path: Absolute or relative path to File
:return string: Document ID
"""
headers = {
'Access-Token': access_token,
}
data = {
'Mime-Type': 'application/pdf',
'File-Name': 'abc.pdf'
}
url = api_url + '/transientDocuments'
files = {'File': open(file_path, 'rb')}
return requests.post(url, headers=headers, data=data,
files=files).json().get('transientDocumentId')
答案 0 :(得分:2)
这就是它的工作原理
def send_document(file_path, access_token=access_token):
"""Uploads document to EchoSign and returns its ID
:param access_token: EchoSign Access Token
:param file_path: Absolute or relative path to File
:return string: Document ID
"""
headers = {
'Access-Token': access_token,
}
data = {
'Mime-Type': 'application/pdf',
'File-Name': 'abc.pdf'
}
url = api_url + '/transientDocuments'
files = {'File': open(file_path, 'rb')}
return requests.post(url, headers=headers, data=data,
files=files).json().get('transientDocumentId')
答案 1 :(得分:0)
尝试传递文件名和mime-type,如下所示:
files = {
'File': (
os.path.basename(file_path),
f,
'application/pdf',
)
}
参考文献:
man curl
,请参阅--trace-file FILE
argumet