Docusign: Python code not able to upload PDF formats
Question: 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


Answer: What is your terminal setting.... it should be UTF-8. Also, take a look at
this [article](http://www.joelonsoftware.com/articles/Unicode.html) 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](http://legacy.python.org/dev/peps/pep-0263/) docs

