-->

使用文件字节REST API创建的DocuSign信封:“不能转换”(Create Docusign

2019-10-20 23:13发布

我下面的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))。

如何插上字节表示没有首先将其转换为字符串,因为我串联了吗?

任何帮助,将不胜感激。

Answer 1:

解决的办法是一切送入字节:

    def makeBody(file_stream, envelopeDef):
    reqBody = "\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"
    reqBody2 = "\r\n" + \
            "--BOUNDARY--\r\n\r\n"

    body = b"".join([bytes(reqBody, 'utf-8'), file_stream, bytes(reqBody2, 'utf-8')])
    return body

我在的DocuSign支持及其在Python中不显示怎么解决这个文档大失所望。 他们只显示.txt文件的情况。 我也通过电子邮件的支持和他们交谈的即时聊天,使之更清楚,但他们没有回应。



文章来源: Create Docusign envelope with REST API using document bytes: “cannot convert”