【发布时间】:2016-07-03 04:39:40
【问题描述】:
我正在尝试使用 Python 将 PDF 上传到 OneNote。根据 OneNote API,我需要发布这样的请求:
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 中执行多部分请求。我可以做一个基本的 text/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 代码?
[########### 更新############]
根据 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)
[##########更新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 我做了,但它仍然给出同样的错误。即使我使代码更简单,它也会给出格式错误的错误(参见上面的 update2)。
标签: python http request multipart onenote