1

我试图弄清楚如何使用 Python 向 OneNote 写入 MultiPart POST 请求。这是我到目前为止所做的:

url = ROOT_URL+"pages"

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)
prepped = request.prepare()
response = session.send(prepped)

但是,每当我运行它时,都会收到错误响应“多部分有效负载格式错误”。我也试过这样:

url = ROOT_URL+"pages"

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)

一样。甚至超级基础:

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

files = {'file1': ('filename', 'data', 'text/plain')}

r = requests.post(url, headers=headers, files=files)

给出那个错误。我究竟做错了什么?

4

1 回答 1

0

第一个预感:确保请求正文中的换行符是 CRLF(不仅仅是 LF)。multipart的RFC规范对此非常挑剔

于 2016-03-18T20:12:54.373 回答