title: How to parse multipart/form-data forwarded by Mailgun API on AWS Lambda?
body: <p>Here is what I am trying to achieve: Users will be able to email me at test@myDomain.com which is integrated with the Mailgun service. Based off a rule I'll have set Mailgun will forward this email as a POST request to https:url-to-my-lambda-function.amazonaws.com/email. I then receive this POST request in Lambda, parse it, then grab and use the html from the email for further work.</p>
  
  <p><strong>The issue I am running into is that Mailgun sends their POSTs not as JSON but as a multipart/form-data.</strong> I have not been able to find a way to parse this content type and grab the email html. Here is a <a href="http://bin.mailgun.net/ec2835ad#8zg7" rel="nofollow noreferrer">sample</a> forwarded email from Mailgun (note that technically mailgun sends along attachments too, but all I care about is the stripped-html field).</p>
  
  <p>Also note that I am working with the Serverless framework and need to write this function in either Python or Node.js.</p>
  
  <p><strong>Things I have tried to do to parse the POST request:</strong></p>
  
  <ul>
  <li>Tried using the <a href="https://www.npmjs.com/package/parse-multipart" rel="nofollow noreferrer">parse-multipart</a> npm library, but was unable to get it to work I think because I couldn't find the boundary string. This might still be a tenable solution.</li>
  <li>Tried <code>let body = new String(decodeURIComponent(event.body));</code> , to put the data into a string and decode it because it seemed to be coming in URL encoded. Then I was using regex to find and pull out the stripped-html that I wanted. However, I think the URL decoding wasn't properly working because it was converting spaces into pluses. See the block below as an example of what I got:</li>
  </ul>
  
  <pre><code>&lt;td+align="center"+valign="top"+id="m_-6910385412628668961m_-3845437051063103019m_-3682438291175620773bodyCell"+style="padding-bottom:40px;height:100%;margin:0;padding:0;width:100%;border-top:0"&gt;\n++++++++++++++++++++++++\n++++++++++++++++++++++++&lt;table+border="0"+cellpadding="0"+cellspacing="0"+width="100%"+style="border-collapse:collapse"&gt;\n++++++++++++++++++++++++++++&lt;tbody&gt;&lt;tr&gt;\n++++++++++++++++++++++++++++++++&lt;td+align="center"+valign="top"&gt;\n++++++++++++++++++++++++++++++++++++\n++++++++++++++++++++++++++++++++++++&lt;table+border="0"+cellpadding="0"+cellspacing="0"+width="100%"+id="m_-6910385412628668961m_-3845437051063103019m_-3682438291175620773templatePreheader"+style="border-collapse:collapse;background-color:#eaeaea;border-top:0;border-bottom:1px+solid+#d0d0d0"&gt;\n++++++++++++++++++++++++++++++++++++++++&lt;tbody&gt;&lt;tr&gt;\n++++++++++++++++++++++++++++++++++++++++\t&lt;td+align="center"+valign="top"&gt;\n++++++++++++++++++++++++++++++++++++++++++++++++&lt;table+border="0"+cellpadding="0"+cellspacing="0"+width="600"+style="border-collapse:collapse"&gt;
  </code></pre>
  
  <ul>
  <li>I have also tried using libraries like <a href="https://www.npmjs.com/package/busboy" rel="nofollow noreferrer">busboy</a> to decode the multipart/form-data but have had issues with getting them working in Lambda because they work off of requests.</li>
  </ul>
  
  <p>How can I parse this POST request? Is this even possible?</p>