我下面的API演练在这里创建一个信封使用Python: http://iodocs.docusign.com/APIWalkthrough/requestSignatureFromDocument
该过程正常工作与简单的文本文件。 举例来说,如果我创建一个文本文件“file.txt的”,我可以打电话:
with open('file.txt', 'r') as f:
file_stream = f.read()
这file_stream正常工作与我现有的代码:
def makeBody(file_stream, envelopeDef):
body = "\r\n\r\n--BOUNDARY\r\n" + \
"Content-Type: application/json\r\n" + \
"Content-Disposition: form-data\r\n" + \
"\r\n" + \
envelopeDef + "\r\n\r\n--BOUNDARY\r\n" + \
"Content-Type: application/pdf\r\n" + \
"Content-Disposition: file; filename=\"thesis.pdf\"; documentId=1\r\n" + \
"\r\n" + \
file_stream + "\r\n" + \
"--BOUNDARY--\r\n\r\n"
return body
def envelope(res):
envelopeDef = "{\"emailBlurb\":\"Please sign this.\"," + \
"\"emailSubject\":\"Demo Docusign\"," + \
"\"documents\":[{" + \
"\"documentId\":\"1\"," + \
"\"name\":\"test_doc.pdf\"}]," + \
"\"recipients\":{" + \
"\"signers\":[{" + \
"\"email\":\"email@email.io\"," + \
"\"name\":\"Name\"," + \
"\"recipientId\":\"1\"," + \
"\"clientUserId\":\"1\"," + \
"}]}," + \
"\"status\":\"created\"}"
local_header = res['headers'].copy()
local_header['Content-Type'] = 'multipart/form-data; boundary=BOUNDARY'
url = "%s/envelopes" % res['base_url']
file_stream = ''
with open('thesis.pdf', 'rb') as f:
file_stream = f.read()
file_stream = str(file_stream)
body = DocusignSignerView.makeBody(file_stream, envelopeDef)
resp = requests.post(url, headers=local_header, data=body)
此代码可以产生一个400错误的请求(“数据不能被转换”)。
从我在网上找到的,我需要插件文件的字节表示到请求的主体。 不是字符串表示(STR(file_stream))。
如何插上字节表示没有首先将其转换为字符串,因为我串联了吗?
任何帮助,将不胜感激。