2016-06-30 66 views
0

我正在使用DocuSign給出的示例通過Python使用REST API發送信封。我幾乎複製+粘貼代碼,並分配了用戶名,密碼,收件人信息等(由於顯而易見的原因,它不在代碼塊中)。我的問題是,我得到了INVALID_REQUEST_BODY錯誤,特別是:DocuSign API發送Evnelope Python示例錯誤

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

這裏是代碼:

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'); 
+0

這有什麼幫助嗎? http://stackoverflow.com/questions/25356317/senders-notifications看起來像他遇到了類似的錯誤。 –

回答

3

這一行:

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

它有一個額外的 'd' 字符。刪除它工作。

+0

很酷,感謝發佈。你能接受這個作爲社區利益的答案嗎?謝謝 – Ergin