如何使用Python向OneNote发送多部分PDF请求

时间:2016-03-17 01:49:47

标签: python http request multipart onenote

我尝试使用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中执行多部分请求。我可以做一个基本的文本/ 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的建议,我尝试了以下方法。当我这样做时,我得到的错误从&#34;页面创建请求切换要求内容是多部分的,具有演示部分&#34; to&#34;多部分有效负载格式错误。&#34;我认为这是因为我实际上没有在某处附加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)

2 个答案:

答案 0 :(得分:2)

答案结果是Python将空行编码为&#34; \ n&#34;但OneNote需要&#34; \ r \ n&#34;。在最终边界之后还需要一个空行(&#34; \ r \ n&#34;)。最后,它不能在Content-Type和Content-Disposition行的正文中有任何前导空格(无缩进)。 (每个Content-Disposition行之后也应该有一个空行。)

例如,如果这是身体:

"""--MyBoundary
Content-Type: text/html
Content-Disposition: form-data; name="Presentation"

Some random text
--MyBoundary
Content-Type: text/text
Content-Disposition: form-data; name="more"; filename="more.txt"

More text
--MyBoundary--
"""

它应该由字符串

表示
'--MyBoundary\r\nContent-Type: text/html\r\nContent-Disposition: form-data; name="Presentation"\r\n\r\nSome random text\r\n--MyBoundary\r\nContent-Type: text/text\r\nContent-Disposition: form-data; name="more"; filename="more.txt"\r\n\r\nMore text\r\n--MyBoundary--\r\n' 

只需在三个&#34;&#34;&#34;内输入文字即可。引号(将在最后的字符串中自动创建\ n,无论哪里有空行)然后替换&#34; \ n&#34;用&#34; \ r \ n&#34;:

body = body.replace("\n", "\r\n")

标题是:

headers = {"Content-Type":"multipart/form-data; boundary=MyBoundary",
           "Authorization" : "bearer " + access_token} 

最后,您可以像这样发布呼叫:

session = requests.Session()
request = requests.Request(method="POST", headers=headers,
                           url=url,  data=body)
prepped = request.prepare()
response = session.send(prepped)

答案 1 :(得分:0)

您需要构建完整的HTTP请求,而不仅仅是发送HTML。

对于你的身体,尝试在问题中发布完整的身体。

--MyAppPartBoundary
Content-Disposition:form-data; name="Presentation"
Content-type:text/html

<!DOCTYPE html>
<html>
  // truncated
</html>

--MyAppPartBoundary
Content-Disposition:form-data; name="OfficeLeasePartName"
Content-type:application/pdf

... PDF binary data ...

--MyAppPartBoundary--

确保正确设置内容类型标题:

Content-Type:multipart/form-data; boundary=MyAppPartBoundary