How should I provide DocuSign with a PDF?

We're using Python and the Requests library to add PDFs to DocuSign envelopes using the Add document method of the REST API v2:

response = requests.put(
    '<base URL>/envelopes/<envelope ID>/documents/<next document ID>',
    files={'document': <the PDF file object>},  # <- added to the request's body
    headers=self._get_headers(
        {
            'Content-Disposition': 'document; filename="the-file.pdf";'
        }
    ),
    timeout=60
)

This has worked for us in most cases, except that about 1 in 100 PDFs isn't accepted via the API. When this problem occurs we tell our users to upload the PDFs directly through the DocuSign UI, which works. This prompted us (with the help of support) to look at the Document params link that appears above the example request on the Add document page linked above. The page shows a documentBase64 attribute, and a number of other fields. How can I supply the document in this format, with all the fields specified? Should I replace the file in my call above with files={'document': <JSON-encoded object>} or? I can't figure out how to add a document OTHER than the way we're currently doing it. Is there another way I'm missing?

Upvotes: 0

Views: 1378

Answers (1)

Kim Brandl
Kim Brandl

Reputation: 13500

It looks like there are now two different ways to add a document to a Draft Envelope with the REST API:

  1. Use a multi-part request, where the first part contains the JSON body and each subsequent part contains a document's bytes -- in un-encoded format. An example of this approach is shown on pages 136-137 of the REST API guide (http://www.docusign.com/sites/default/files/REST_API_Guide_v2.pdf).

  2. Use a normal request (i.e., not multi-part request), and supply document bytes in base64-encoded format as the value of the documentBase64 property for each document object in the Request. (This looks to be new as of the recent December 2013 API release/update.)

Based on the info you've included in your question, I suspect you're currently using approach #1. As described above, the main difference between the two approaches is the general structure of the request, and ALSO -- approach #1 expects document bytes to be un-encoded, while approach #2 expects document bytes to be base64-encoded. I suspect your issue has to do with encoding of files. i.e., if you're using approach #1 and any of the files are encoded, you'll likely have issues.

Upvotes: 4

Related Questions