wbrugato
wbrugato

Reputation: 1469

DocuSign API Send Evnelope Python Example Error

I am using the example given by DocuSign to send an envelope using the REST API via Python. I pretty much copy+pasted the code, and assigned the username, password, recipient info, etc (it is not in the code block for obvious reasons). My issue is that I am getting an INVALID_REQUEST_BODY error, specifically:

The request body is missing or improperly formatted. Additional text encountered after finished reading JSON content: -. Path '', line 3, position 1.

Here is code:

 import httplib2, json, sys


authenticateStr = "<DocuSignCredentials>" \
          "<Username>" + username + "</Username>" \
                                    "<Password>" + password + "</Password>" \
                                                              "<IntegratorKey>" + integratorKey + "</IntegratorKey>" \
                                                                                                  "</DocuSignCredentials>";


#
# STEP 1 - Login
#
url = 'https://demo.docusign.net/restapi/v2/login_information';
headers = {'X-DocuSign-Authentication': authenticateStr, 'Accept': 'application/json'};
http = httplib2.Http();
response, content = http.request(url, 'GET', headers=headers);

status = response.get('status');
if (status != '200'):
    print("Error calling webservice, status is: %s" % status);
    sys.exit();

# get the baseUrl and accountId from the response body
data = json.loads(content);
loginInfo = data.get('loginAccounts');
D = loginInfo[0];
baseUrl = D['baseUrl'];
accountId = D['accountId'];

envelopeDef = "{\"emailBlurb\":\"This comes from Python\"," + \
              "\"emailSubject\":\"API Call for adding signature request to document and sending\"," + \
              "\"documents\":[{" + \
              "\"documentId\":\"1\"," + \
              "\"name\":\"test_doc.txt\"}]," + \
              "\"recipients\":{" + \
              "\"signers\":[{" + \
              "\"email\":\"" + signer + "\"," + \
              "\"name\":\"Name\"," + \
              "\"recipientId\":\"1\"," + \
              "\"tabs\":{" + \
              "\"signHereTabs\":[{" + \
              "\"xPosition\":\"100\"," + \
              "\"yPosition\":\"100\"," + \
              "\"documentId\":\"1\"," + \
              "\"pageNumber\":\"1\"" + "}]}}]}," + \
              "\"status\":\"sent\"}";

# convert the file into a string and add to the request body
fileContents = open("test_doc.txt", "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\Dr\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': authenticateStr, 'Content-Type': 'multipart/form-data; boundary=BOUNDARY',
           'Accept': 'application/json'};
http = httplib2.Http();
response, content = http.request(url, 'POST', headers=headers, body=requestBody);
status = response.get('status');
if (status != '201'):
    print("Error calling webservice, status is: %s\nError description - %s" % (status, content));
    sys.exit();
data = json.loads(content);
envId = data.get('envelopeId');

Upvotes: 0

Views: 403

Answers (1)

wbrugato
wbrugato

Reputation: 1469

this line:

envelopeDef + "\r\n\r\n--BOUNDARY\Dr\n" + \

Had an extra 'D' character in it. Removing it worked.

Upvotes: 3

Related Questions