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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (1100)

我正在使用文件附件自动发送eamil的python程序。

sendHTML是电子邮件内容。

但是,添加下面的原始源文件会导致error : Data must not be a string。所以,我已经将代码更改为如下所示,但它也无效。

    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 d7a71dbb2d2cb6cd9f70126a926f6a4c',  "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)
提问于
用户回答回答于
headers = {'authorization': 'Bearer d7a71dbb2d2cb6cd9f70126a926f6a4c',  "Content-Type" : "multipart/form-data"}
files = {'file': open('1', 'rb')}
res = requests.post(url, json=sMailBody, headers=headers, files=files)

扫码关注云+社区

领取腾讯云代金券