簡體   English   中英

Python multipart / form-data請求錯誤:數據不能為字符串

[英]Python multipart/form-data request error : Data must not be a string

我正在制作python程序,用於自動發送帶有文件附件的eamil。

sendHTML是電子郵件內容。 沒有附件,效果很好。

但是,添加類似以下原始來源的文件會導致錯誤:數據不能為字符串。 因此,我將代碼更改為如下所示,但它也無法正常工作。 請救救我:(

==============更改代碼================================= ================

    files = {'json' : (None, json.dumps(sMailBody).encode("utf-8"), 'application/json' ),'file': (open('''20180430_'18 Tizen Store Buyer Report v2.4.xlsx''', 'rb'), 'application/octet-stream')}
    headers = {'authorization': 'Bearer xx',  "Content-Type" : "multipart/form-data"}
    res = requests.post(url, headers=headers, files=files)

==================原始來源========================

sendHTML = str(fullReportFrameSoup)                                    -
url = "the url of samsung"
sMailBody = {         
--> this is my company mail system's  Rest API                                                       
   "resourceVO" :
   {
   "email" : "xxx@samsung.com",
           "localeStr" : "ko_KR",
            "encoding" : "utf-8",
            "timeZone" : "GMT+9",
        },
    "sendMailVO" :
        {
            "contentText" : sendHTML,
            "subject" : "[Tizen Store] 일일통계",
            "docSecuType" : "PERSONAL",
            "contentType" : "HTML",
            "from" :
                   {
                    "email" : "xxx@samsung.com"
                   },
            "attachs" :[],
            "recipients" :
                [
                   {
                        "email" : "xxx@samsung.com",
                        "recipientType" : "TO"
                    }
                ]
        }
}



json_data = json.dumps(sMailBody).encode("utf-8")
files = {'file': ('''20180430_'18 Tizen Store Buyer Report v2.4.xlsx''', open('''20180430_'18 Tizen Store Buyer Report v2.4.xlsx''', 'rb') , 'application/vnd.ms-excel', {'Expires': '0'})}-->  
headers = {'authorization': 'Bearer xxx', "Content-Type" : "application/json"}
res = requests.post(url, data=json_data, headers=headers, files=files)

嘗試將JSON有效負載(sMailBody)作為json參數中的dict傳遞:

headers = {'authorization': 'Bearer d7a71dbb2d2cb6cd9f70126a926f6a4c',  "Content-Type" : "multipart/form-data"}
files = {'file': open('1', 'rb')}
res = requests.post(url, json=sMailBody, headers=headers, files=files)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM