How to parse multipart/form-data forwarded by Mailgun API on AWS Lambda? - amazon-web-services

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.
The issue I am running into is that Mailgun sends their POSTs not as JSON but as a multipart/form-data. I have not been able to find a way to parse this content type and grab the email html. Here is a sample forwarded email from Mailgun (note that technically mailgun sends along attachments too, but all I care about is the stripped-html field).
Also note that I am working with the Serverless framework and need to write this function in either Python or Node.js.
Things I have tried to do to parse the POST request:
Tried using the parse-multipart 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.
Tried let body = new String(decodeURIComponent(event.body)); , 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:
<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">\n++++++++++++++++++++++++\n++++++++++++++++++++++++<table+border="0"+cellpadding="0"+cellspacing="0"+width="100%"+style="border-collapse:collapse">\n++++++++++++++++++++++++++++<tbody><tr>\n++++++++++++++++++++++++++++++++<td+align="center"+valign="top">\n++++++++++++++++++++++++++++++++++++\n++++++++++++++++++++++++++++++++++++<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">\n++++++++++++++++++++++++++++++++++++++++<tbody><tr>\n++++++++++++++++++++++++++++++++++++++++\t<td+align="center"+valign="top">\n++++++++++++++++++++++++++++++++++++++++++++++++<table+border="0"+cellpadding="0"+cellspacing="0"+width="600"+style="border-collapse:collapse">
I have also tried using libraries like busboy to decode the multipart/form-data but have had issues with getting them working in Lambda because they work off of requests.
How can I parse this POST request? Is this even possible?

You should probably consider using a module designed to parse multipart form data such as https://www.npmjs.com/package/parse-formdata.

Related

How are text messages added to html messages

I'm using cURL to send Mailgun messages. One of the cURL arguments is -F template='mytemplate' \, which uses a template set-up on Mailgun. How do I add a text version of the message?
According to the MailGun API docs, you can request a text-only version by including the parameter -F t:text='yes' as part of your cURL request.
This will change the MIME Content-Type from text/html to multipart/alternative which most mail clients will render first as HTML, falling back to text if there's a problem. It's worth noting however, that this leaves the text interpretation up to MailGun meaning that you don't get any say in how the text is composed.
I've tested this a few times and ran into minimal issues, but if you want to ensure that your text comes through as intended, you're better off composing your own multipart/alternative MIME message and passing that to MailGun using the /messages.mime API endpoint.

POST request to Django DRF call working in cURL but not with Postman

I'm following the instructions to support TokenAuthentication in my rest-api site, shown here. Using cURL, I have been able to obtain my user's token (username - example, password - example), through the following command:
curl -X POST -d "username=example&password=example" localhost:8000/api/login/
This returns a successful response, with example's authentication token.
Yet when I do (what I think is) the same thing through Postman, it simply does not work. See image below.
From the error code (400 - Bad request), it seems like it's not even receiving the POST parameters at all. Can anyone help me here?
See your URL in postman. There is attached query String with the URL.So remove that query String from the URL and send parameters as a post request like this.
http://localhost:8000/api/login/
Even this is very old question, but if this answer would be helpful...
I had exactly same issue
solution:
don't put username and password in address bar,but only
and in body put json data of your username and password as below
be careful, don't use single quotation marks'', but use double quotation marks "" instead, otherwise will fail, no clue why
Depending on how your API is set up, you probably need to specify the content type in your request headers, Content-Type: application/json.

Issue connecting SOAP through Postman

I’m stuck with trying to connect to my SOAP API. The goal is to retrieve a quote via the “Getquote” function which is available in our webservice and use that quote in an application in Bubble.is. Therefore, I want to make it work through form-data so I can reuse the keys and values in Bubble. I get a succesfull quote through the raw method. See picture
Raw method:
You can see that all my fields are in the body so with the form-data method I put all the individual fields in key and value but I get the error message you see below.
Form data method:
Can someone see what I'm doing wrong? Excuses me for I am just starting. There might be some beginner mistakes in there. Thanks for the help!
SOAP encodes messages by using XML. Form data uses a completely different encoding, which the SOAP server doesn't understand, hence the error.
Although I've never used it, there is a Chrome extension called Boomerang that supports SOAP requests, and which may suit you better.

How to send POST variable in POSTMAN

I can't get POSTMAN to send any post variables to my Django app. Suppose I have a post variable called 'report_request' and it has a value that is a JSON string. On the Django side I want to get request.POST['report_request'] and parse the JSON into a dictionary. But POSTMAN never seems to send the POST data. How exactly do I do this? Is there some magical header I need to send?
Doh! My bad. The URL I need to connect to is really HTTPS rather than HTTP, but I was specifying the URL as http://. Apparently if Postman is asked to connect to an HTTPS site using HTTP, it silently just drops all POST variables. How lovely. Anyway it was an easy fix, just change the http:// url to https:// and all is well.
Be sure to provide the POST data in the body (body tab) of the request and not in the parameters (params tab).
Otherwise, POSTMAN will interpret your POST request as being without data and on a url with GET parameters.
See these specifications about csrf if needed
Check if you're sending the csrf token, it's a security feature.
https://docs.djangoproject.com/en/1.8/ref/csrf/

How post data over https with urllib2?

I want to integrate a credit card processing in my website using Paybox.com API's.
I have to send a POST request (using urllib2) to Paybox API's with credit card details (number, date, cvv) when a user submit a form.
How can I secure that? is it enougth to put https://www.mywebsite.com/card/processing in my form action?
How can I send POST data over HTTPS using urllib2?
PS: I work on Django.
Well in terms of security refer to this QA: POST data encryption - Is HTTPS enough?
As far as how to do it, here's an explanation about using urllib: http://www.codercaste.com/2009/11/28/how-to-use-the-urllib-python-library-to-fetch-url-data-and-more/
The idea is to use the urlencode command to create a parameters object for the request, then create a request object from the url and the parameters object, and then call urlopen on the request object in order to actually send the request.
Here are solutions using python-request lib: http://www.python-requests.org/en/latest/user/advanced/
request using ssl: http://docs.python-requests.org/en/latest/user/advanced/#ssl-cert-verification
request using post: http://docs.python-requests.org/en/latest/user/quickstart/#more-complicated-post-requests (should also allow verify=True parameter)
By the way, python-request is a very powerful and easy way to make requests.