且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用文档字节使用REST API创建Docusign信封:

更新时间:2022-10-14 18:58:06

解决方案是将所有内容发送为字节:

  def makeBody(file_stream,信封Def):
reqBody = = r\n\r\n--BOUNDARY\r\ n + \
内容类型:application / json\r\n + \
内容处置:form-data\r\n + \
\r\n + \
信封Def + + r\n\r\n--BOUNDARY\r\n + \
内容类型:application / pdf\r\n + \
内容处置:文件;文件名= 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')])
返还身体

我对Docusign支持及其文档没有在Python中显示如何解决这一问题感到非常失望。它们仅显示.txt文件大小写。我还通过电子邮件发送了支持,并通过实时聊天与他们进行了交流,以使情况更清楚,但他们没有回应。


I am following the API walkthrough for creating an envelopes here using Python: http://iodocs.docusign.com/APIWalkthrough/requestSignatureFromDocument

The process works fine with a simple text file. For instance, if I create a text file "file.txt", I can call:

with open('file.txt', 'r') as f:
    file_stream = f.read()

This file_stream works fine with my existing code:

    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)

This code yields a 400 BAD Request ("data cannot be converted").

From what I've found online, I need to plugin the bytes representation of the file INTO the body of the request. NOT the string representation ( str(file_stream) ).

How do I plug in the bytes representation without first converting it into a string since I am concatenating it?

Any help would be appreciated.

The solution was to send everything into bytes:

    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

I'm sorely disappointed in Docusign support and their documentation for not showing in Python how to solve this. They only show the .txt file case. I have also emailed support and talked to them on Live Chat to make this clearer, but they have not responded.