How to Use Python to Send a Multipart PDF Request to OneNote
<p>I'm trying to upload a PDF to OneNote using Python. According to the OneNote API, I need to post a request like this:</p>

<pre><code>Content-Type:multipart/form-data; boundary=MyAppPartBoundary
Authorization:bearer tokenString

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

&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;title&gt;A page with an embedded and displayed PDF file&lt;/title&gt;
  &lt;/head&gt;
    &lt;body&gt;
      &lt;p&gt;Attached is the lease agreement for the expanded offices!&lt;/p&gt;
      &lt;object 
        data-attachment="OfficeLease.pdf" 
        data="name:OfficeLeasePartName" 
        type="application/pdf" /&gt;
      &lt;p&gt;Here's the contents of our new lease.&lt;/p&gt;
      &lt;img data-render-src="name:OfficeLeasePartName" width="900"/&gt;
    &lt;/body&gt;
&lt;/html&gt;

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

... PDF binary data ...

--MyAppPartBoundary--
</code></pre>

<p>However, I have no idea how to do a multipart request in Python. I can do a basic text/html request just fine though:</p>

<pre><code>url = ROOT_URL+"pages"

headers = {"Content-Type":"text/html",
           "Authorization" : "bearer " + access_token}

# Format html (title &amp; text)

html = "&lt;html&gt;&lt;head&gt;&lt;title&gt;" + title + "&lt;/title&gt;&lt;/head&gt;"
html += "&lt;body&gt;&lt;p&gt;" + text + "&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;"

# Send request

session = requests.Session()
request = requests.Request(method="POST", headers=headers,
                           url=url,  data=html)
prepped = request.prepare()
response = session.send(prepped)
</code></pre>

<p>How would I modify that Python code for multi-part?</p>

<p>[########### UPDATE ############]</p>

<p>Based on jayongg's suggestion, I tried the following. When I do, the error I get switches from "Page create requests require the content to be multipart, with a presentation part" to "The multi-part payload was malformed." I think it's because I'm not actually attaching the pdf file somewhere? I'm also not sure what's the difference between OfficeLease.pdf in the OneNote api example and OfficeLeasePartName.</p>

<p>Here is my current code:</p>

<pre><code>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

        &lt;!DOCTYPE html&gt;
        &lt;html&gt;
          &lt;head&gt;
            &lt;title&gt;A page with an embedded and displayed PDF file&lt;/title&gt;
          &lt;/head&gt;
            &lt;body&gt;
              &lt;p&gt;Attached is the lease agreement for the expanded offices!&lt;/p&gt;
              &lt;object 
                data-attachment="Sample5.pdf" 
                data="name:Sample5" 
                type="application/pdf" /&gt;
              &lt;p&gt;Here's the contents of our new lease.&lt;/p&gt;
              &lt;img data-render-src="name:Sample5" width="900"/&gt;
            &lt;/body&gt;
        &lt;/html&gt;

        --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)
</code></pre>

<p>[########## UPDATE 2 ##############]</p>

<p>If I make the code even simpler, it still results in a malformed error:</p>

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

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

        &lt;!DOCTYPE html&gt;
        &lt;html&gt;
          &lt;head&gt;
            &lt;title&gt;One Note Text&lt;/title&gt;
          &lt;/head&gt;
            &lt;body&gt;
              &lt;p&gt;Hello OneNote World&lt;/p&gt;
            &lt;/body&gt;
        &lt;/html&gt;

        --MyAppPartBoundary--
        """

session = requests.Session()
request = requests.Request(method="POST", headers=headers,
                           url=url,  data=txt)
</code></pre>

<p>I've also tried like this. Same thing:</p>

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

txt = """&lt;!DOCTYPE html&gt;
        &lt;html&gt;
          &lt;head&gt;
            &lt;title&gt;One Note Text&lt;/title&gt;
          &lt;/head&gt;
            &lt;body&gt;
              &lt;p&gt;Hello OneNote World&lt;/p&gt;
            &lt;/body&gt;
        &lt;/html&gt;"""    

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)
</code></pre>