我试图使用Python将PDF上传到OneNote。根据OneNote API,我需要发布这样的请求:如何使用Python将多部分PDF请求发送到OneNote
Content-Type:multipart/form-data; boundary=MyAppPartBoundary
Authorization:bearer tokenString
--MyAppPartBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
<html>
<head>
<title>A page with an embedded and displayed PDF file</title>
</head>
<body>
<p>Attached is the lease agreement for the expanded offices!</p>
<object
data-attachment="OfficeLease.pdf"
data="name:OfficeLeasePartName"
type="application/pdf" />
<p>Here's the contents of our new lease.</p>
<img data-render-src="name:OfficeLeasePartName" width="900"/>
</body>
</html>
--MyAppPartBoundary
Content-Disposition:form-data; name="OfficeLeasePartName"
Content-type:application/pdf
... PDF binary data ...
--MyAppPartBoundary--
但是,我不知道如何在Python中执行多部分请求。我可以做一个基本的文本/ HTML请求就好了:
url = ROOT_URL+"pages"
headers = {"Content-Type":"text/html",
"Authorization" : "bearer " + access_token}
# Format html (title & text)
html = "<html><head><title>" + title + "</title></head>"
html += "<body><p>" + text + "</p></body></html>"
# Send request
session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, data=html)
prepped = request.prepare()
response = session.send(prepped)
我该如何修改多部分的Python代码?
[########### UPDATE ############]
基于jayongg的建议下,我尝试以下。当我这样做时,我从“页面创建请求”中切换的错误需要内容为多部分,并将“演示文稿部分”改为“多部分有效内容格式错误”。我认为这是因为我实际上并没有将pdf文件附加到某处?我也不确定OneNote api示例中的OfficeLease.pdf和OfficeLeasePartName之间的区别。
这是我的当前代码:
url = ROOT_URL+"pages"
path = os.path.join(pdfFolder, pdfName + ".pdf")
headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary",
"Authorization" : "bearer " + access_token}
f = open(path, "rb").read()
txt = """--MyAppPartBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
<html>
<head>
<title>A page with an embedded and displayed PDF file</title>
</head>
<body>
<p>Attached is the lease agreement for the expanded offices!</p>
<object
data-attachment="Sample5.pdf"
data="name:Sample5"
type="application/pdf" />
<p>Here's the contents of our new lease.</p>
<img data-render-src="name:Sample5" width="900"/>
</body>
</html>
--MyAppPartBoundary
Content-Disposition:form-data; name="OfficeLeasePartName"
Content-type:application/pdf
""" + f + """
--MyAppPartBoundary--"""
session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, data=txt)
prepped = request.prepare()
response = session.send(prepped)
[########## UPDATE 2 ##############]
如果我使代码更简单,它仍然导致格式错误:
headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary",
"Authorization" : "bearer " + access_token}
txt = """--MyAppPartBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html
<!DOCTYPE html>
<html>
<head>
<title>One Note Text</title>
</head>
<body>
<p>Hello OneNote World</p>
</body>
</html>
--MyAppPartBoundary--
"""
session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, data=txt)
我也试过这样。同样的事情:
headers = {"Content-Type":"multipart/form-data; boundary=MyAppPartBoundary",
"Authorization" : "bearer " + access_token}
txt = """<!DOCTYPE html>
<html>
<head>
<title>One Note Text</title>
</head>
<body>
<p>Hello OneNote World</p>
</body>
</html>"""
files = {'file1': ('Presentation', txt, 'text/html')}
session = requests.Session()
request = requests.Request(method="POST", headers=headers,
url=url, files=files)
prepped = request.prepare()
response = session.send(prepped)
尝试用最后--MyAppPartBoundary-- – jayongg
@jayongg我做了后加入一个换行符,但它仍然给出同样的错误。即使我使代码更简单,它也会给出错误的错误(参见上面的update2)。 – Elliptica