2014-09-12 83 views
0

我的代碼:添加字節流爲請求主體

with open('thesis.pdf', 'rb') as f: 
    filename = f.read() 

我想作爲指定在這裏的字節添加到請求的DocuSign:Very basic stuff - how do I send a document to docusign via REST API? I only see <document bytes go here...> everywhere

「DO NOT字節流轉換爲字符串在將其添加到DocuSign請求之前 - 字節流應直接寫入請求中。「

def makeBody(file_stream, envelopeDef): 
    body = "\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: application/pdf\r\n" + \ 
      "Content-Disposition: file; filename=\"thesis.pdf\"; documentId=1\r\n" + \ 
      "\r\n" + \ 
      file_stream + "\r\n" + \ 
      "--BOUNDARY--\r\n\r\n" 
    return body 

如在指定:http://iodocs.docusign.com/APIWalkthrough/requestSignatureFromDocument

然而,字節類不能被隱式轉換爲字符串。那麼如何直接將字節流寫入請求?

回答