首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python multipart/form-data请求错误:数据不能是字符串

Python multipart/form-data请求错误:数据不能是字符串
EN

Stack Overflow用户
提问于 2018-05-31 17:26:19
回答 1查看 1.6K关注 0票数 0

我正在制作python程序,用于自动发送带有文件附件的eamil。

sendHTML是电子邮件内容。在没有附加的情况下,它工作得很好。

但是,添加像下面这样的源文件会导致错误: Data不能是字符串。所以,我把代码改成了像下面这样,但它也不起作用。请救救我:(

===============changed code=================================================

代码语言:javascript
复制
    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)

===================源码========================

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2018-05-31 19:43:47

尝试在JSON参数中将json有效负载(sMailBody)作为dict传递:

代码语言:javascript
复制
headers = {'authorization': 'Bearer d7a71dbb2d2cb6cd9f70126a926f6a4c',  "Content-Type" : "multipart/form-data"}
files = {'file': open('1', 'rb')}
res = requests.post(url, json=sMailBody, headers=headers, files=files)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50620946

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档