1

I have implemented embedded signing using python(Have followed the code given in Docusign samples). Everything works good with .txt files.

Just any other format gives me an encoding error.

UnicodeDecodeError: 'ascii' codec can't decode byte 0x9c in position 351: ordinal not in range(128)

Code:

filepath = os.path.join(settings.MEDIA_ROOT, documentName)

    fileContents = open(filepath, "r").read()

    requestBody = "\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: text/plain\r\n" + \
                  "Content-Disposition: file; filename=\"test_doc.txt\"; documentId=1\r\n" + \
                  "\r\n" + \
                  fileContents + "\r\n" + \
                  "--BOUNDARY--\r\n\r\n"
    
    # append "/envelopes" to the baseUrl and use in the request
    url = baseUrl + "/envelopes";
    headers = {'X-DocuSign-Authentication': AUTHENTICATION_STR, 'Content-Type': 'multipart/form-data; boundary=BOUNDARY',
               'Accept': 'application/json'};
    http = httplib2.Http()

    response, content = http.request(url, 'POST', headers=headers, body=requestBody)

I tried encoding in UTF-8

 import codecs
    fileContents = codecs.open(filepath,mode='r', encoding='utf-8').read()

Still it doesnt help.

I also tried changing the Content-Type to : application/pdf

Can anyone suggest a way of doing it?

Docusign

Community
  • 1
  • 1
KetanJogani
  • 306
  • 2
  • 20

3 Answers3

0

What is your terminal setting.... it should be UTF-8. Also, take a look at this article to get more clear picture about unicode.

Try this:

import sys
reload(sys)
sys.setdefaultencoding("utf-8")

EDIT:

Defining source code encoding also helps me a lot like :

# -*- coding: utf-8 -*-

Take a look at PEP 0263 docs

Alex
  • 3,167
  • 6
  • 35
  • 50
0

Depending on the file format (PDF, for example) you likely need to handle the content as binary bytes - not unicode text. Generally you'll want to open the file in binary mode: open(filepath, "rb").read(). This prevents conversion of the file's contents into unicode text and returns a byte str instead.

Jeff Kyllo
  • 698
  • 3
  • 8
0

I hit this issue too, and found the issue was this:

fileContents = open(filepath, "r").read()

Should be (read binary):

fileContents = open(filepath, "rb").read()

Cheers

Richard

neophytte
  • 648
  • 2
  • 11
  • 25