使用文档字节使用REST API创建Docusign信封: [英] Create Docusign envelope with REST API using document bytes: "cannot convert"

查看:83
本文介绍了使用文档字节使用REST API创建Docusign信封:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在遵循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 + \
内容类型:application / json\r\n + \
内容-处置:form-data\r\n + \
\r\n + \
信封Def + \r\n\r\n --BOUNDARY\r\n + \
内容类型:application / pdf\r\n + \
Content-Disposition:文件;文件名= \ thesis.pdf\; documentId = 1\r\n + \
\r\n + \
file_stream + \r\n + \
--BOUNDARY--\r\n\r\n
返回实体

def信封(res):
walletDef = {\ emailBlurb\:\请在此签名。\, + \
\ emailSubject\:\ Demo Docusign\, + \
\ documents\:[{ + \
\ documentId \:\ 1\, + \
\ name\:\ test_doc.pdf\}], + \
\收件人\:{ + \
\签名人\:[{ + \
\电子邮件\:\电子邮件@ email.io\, + \
\ name\:\ Name\, + \
\ recipientId\:\ \ 1\, + \
\ clientUserId\:\ 1\, + \
}]}, + \
\状态\:\已创建\}
local_header = res ['headers']。copy()
local_header ['Content-Type'] = '多部分/表单数据; boundary = BOUNDARY'
url =%s / envelopes%res ['base_url']

file_stream =''
带有open('thesis.pdf','rb ')作为f:
file_stream = f.read()
file_stream = str(file_stream)
正文= DocusignSignerView.makeBody(file_stream,envelopeDef)
resp = requests.post( url,headers = local_header,data = body)

此代码产生一个400 BAD Request(数据不能被转换)。



根据我在网上找到的内容,我需要将文件的字节表示形式插入请求的正文中。不是字符串表示形式(str(file_stream))。



既然我是串联的,该如何插入字节表示而不先将其转换成字符串?



解决方案

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

  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.

这篇关于使用文档字节使用REST API创建Docusign信封:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆