Cfhttp and mailgun api - coldfusion

I’ve been trying to configure Mailgun API with Coldfusion's Cfhttp and can’t figure out the format to pass the variables. I configured it in about 5 minutes with Python, and have to roll it to a Coldfusion front end. I’ve used the same basic structure as I did in Python, and it all works great in Python.
I’ve been able to make a connection, but I have not been able to send mail. I know it has to do with the structure, and I can’t find a working example anywhere.
How do you build the cfhttp to pass the required data? Is the data=data I would use in the request in Python the same as a cfhttpparam in ColdFusion? And is it a header, or a body?
My Coldfusion code is below, i've replaced secret information with domain.com
<cfscript>
mailgun_variables = '{
"company_id": 1008,
"company_contact": "Joe Smith",
"random_key": "91A303C8-91FB-AA1D-DE07F18782594721"
}';
data= '{
"from": "email#domain.com",
"to": "email#domain.com",
"subject": "Subject line",
"template": "welcome",
"o:tag": ["welcome", "welcome-countdown", "welcome-alt", "coldfusion"],
"h:X-Mailgun-Variables": #mailgun_variables#}';
</cfscript>
<cfhttp url="https://api.mailgun.net/v3/domain.com/messages"
method="POST"
username="api"
password="APIKEY"
result="response" >
<cfhttpparam type="header" name="Content-Type" value="application/json" />
<cfhttpparam type="header" name="data" value="#serializeJSON(data)#">
</cfhttp>
My error message is a 400 Bad request, {"message":"from parameter is missing"}.
Any guidance or links to examples would be much appreciated. Or maybe you can obviously spot what I am doing wrong?

I ended up having to do a lot of trial and error, but have working code if anyone else ever runs into this issue. Mailgun support was no help.
The answer ended up being that everything had to be passed as cfhttpparam formfields. I also ran into a lot of challenges with tagging and using template variables, but got it all worked out.
I added it to Github for anyone to use, and will continue to improve it there as I get more in-depth with it.
https://github.com/convurt/Mailgun-Coldfusion-Sendmail
cfhttp(method="POST", url="https://api.mailgun.net/v3/DOMAIN.COM/messages", result="response", username="api", password="MAILGUN API KEY") {
cfhttpparam(name="from", type="formfield", value="#email_from#");
cfhttpparam(name="h:Reply-To", type="formfield", value="#reply_to#");
cfhttpparam(name="to", type="formfield", value="#email_to#");
cfhttpparam(name="subject", type="formfield", value="#email_subject#");
// loop over tag list, I could not get it to work correctly as a list
for (t in tags) {
cfhttpparam(name="o:tag", type="formfield", value="#t#");
}
cfhttpparam(name="template", type="formfield", value="#template#");
// custom paramamter for template
cfhttpparam(name="t:variables", type="formfield", value="#t_variables#");

Related

How to return ReportCloud API response into a downloadable file?

I am using ColdFusion to call the ReportCloud API which does a mail merge on the fly based on parameters I send it via cfhttp.
The response from the documentation says:
On success, the HTTP status code in the response header is 200 (OK). The response body contains an array of the created documents encoded as Base64 encoded strings.
Can someone help me with how to turn that response into a downloadable file, either a link or just a straight download? I probably should know this but I don't unfortunately.
Added comments:
Thanks for your help and guidance with this question. I'm using StackOverflow as a poster for the first time, my apologies if the question is vague.
I have put together a code example also to view cffiddle
<!--- Must replace "Authorization" header below with a real key --->
<cfset variables.jsonReq = '{
"mergeData": [
{
"Given_Name": "Mike",
"Surname": "Smith",
"Year_Group": "11"
},
{
"Given_Name": "Sally",
"Surname": "Smith",
"Year_Group": "12"
}
],
"template": null,
"mergeSettings": null
}'>
<cfhttp url="https://api.reporting.cloud/v1/document/merge?returnFormat=DOC&templateName=parentletter.docx" method="post" timeout="20" result="response" file="/www/something.docx">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type="header" name="Authorization" value="ReportingCloud-APIKey oMDM4MrAqL9QEOpyzupnQW5NjvCNtvE5cVDaaLqxI">
<cfhttpparam type="body" name="mergeData" value="#jsonReq#">
</cfhttp>
<cfdump var="#response#">
The response is coming back in what I think is a JSON array. I'm not sure how to go about reading that JSON array and making the contents into a downloadable file.

cfhttp and Stripe APi

I'm trying to get the stripe api working with 'cfhttp' but not having much luck.
<cfhttp
url="https://api.stripe.com/Stripe/Stripe::setApiKey('MyLiveKeyHere');"
authType="Basic" name="StripeAPI" method="Post">
>
<cfoutput>#StripeAPI.statusCode#</cfoutput>
This gives me an error
Message
Invalid CSV line size, expected 1 columns but found 2 instead
Detail
[message: You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY').
I cant find anything online that is helping me - anyone got any ideas?
Thanks
Found out how to do it - through trial and error...
<cfhttp url="https://api.stripe.com/v1/customers/*CustID*" method="Get">
<cfhttpparam type="header" name="Authorization" value="Bearer *Live Key Here*">
</cfhttp>
To authenticate with the Stripe API, you need to use either basic auth or a bearer token rather than placing the API key in the url. For example, to list charges you could do something like this:
<cfhttp url="https://api.stripe.com/v1/charges" method="get" username="sk_test_xxxyyyz">
You'd then want to de-serialize the JSON response.

Error 400: Bad request while fetching json data from instagram api via coldfusion

Afraid to ask this question as I'm not able to create a jsfiddle for it, but hope someone will be able help.
I'm trying to create a cfc in ColdFusion for an instagram login. That part is done. I'm using postman (google app) and by my credentials I can see the user's data in json, but when I'm converting this into ColdFusion it's giving an error. I tried to change the data-type, header and a lot of lines but am still getting the same error again and again.
My code (replaced ids with xxx for security)
<cftry>
<cfhttp url="https://api.instagram.com/oauth/access_token" method="post" resolveurl="yes">
<cfhttpparam type="header" name="Content-Type" value="application/x-www-form-urlencoded" />
<cfhttpparam type="formfield" name="client_id" value="14faxxxxxdc5440f86x6cdd8xxxxf78" />
<cfhttpparam type="formField" name="client_secret" value="40xa78220cfb" />
<cfhttpparam type="formField" name="grant_type" value="authorization_code" />
<cfhttpparam type="formField" name="redirect_uri" value="#URLEncodedFormat('http://example.com/demo/instagramAPI/success.cfm')#" />
<cfhttpparam type="formField" name="code" value="#url.code#" />
</cfhttp>
<cfdump var="#cfhttp#"><cfabort>
<cfcatch type="any">
<cfdump var="#cfcatch#">
</cfcatch>
</cftry>
I'm following this code from this answer. For more info check this . You can see that I'm getting data by using the same login details, but when doing the same via a cfc I'm getting error.
Error which I'm getting after running the url :
I've read a lot of articles and blogs, but still haven't been able to resolve the error. Can anyone help me understand what I'm doing wrong? If you have any other suggestions, please do let me know.
If any additional information is required, just let me know.
Finally i got the answer of my question after days.. Thanx Miguel-F and Mark A Kruger however Mark your link wasn't good for me as that was out of my issue so..
What i did is to update my SSL certificate. I tried before but was not having much information like Organization unit etc but then i followed the steps given in this Link, provided by Miguel and tested then i got expiration code error.
After that i tried to refresh with ?reinit=1 as i made changes in my cfc but forget to reinitialize after updating the certificate, and then i got the result :)
SO final answer is Update your SSL certificate with proper authorization and cfc can fetch data from Instagram..
Link useful for me One, CFC demo. In cfc demo you can download the cfc for instagram which is also useful (even in case you don't have to update the SSL certificate).
If anyone having issue with Instagram cfc then do let me know.. I spent days on this and can help you.. :)

CFhttp to Scrape Image

A contractor has provided us with survey data for a set of stores. The data contains the store numbers, thumbnail images and large images. The data is accessed through the contractor's secured website. In order to build a report for the data, I am trying to scrape the store numbers and images from the site instead of manually downloading each image.
I have not used CFhttp for secured sites, but have had a little success so far with:
<cfhttp
method="post"
url="http://www.website.com/impart/client_login.php"
throwonerror="Yes"
redirect = "yes"
resolveUrl = "yes">
<cfhttpparam name="user" value="myUsername" type="formfield">
<cfhttpparam name="pass" value="myPassword" type="formfield">
<cfhttpparam name="submit" value="Login" type="formfield">
How do I proceed from getting passed the authentication to the page that contains the image to download?
I think that CFHTTP may not be the best choice for this. I am good at BASH, so I would tend towards scripting it with curl, but maybe some product on this page would be easier http://www.timedicer.co.uk/web-scraping ?
What does the dump of cfhttp scope look like? Specifically, what is the status code?
If you get a status code of 200, you'll need to maintain the session as you grab each image. See the following:
http://www.bennadel.com/blog/725-Maintaining-Sessions-Across-Multiple-ColdFusion-CFHttp-Requests.htm
http://www.bennadel.com/projects/cfhttp-session.htm
See this question for saving images via CFHTTP:
Convert an image from CFHTTP filecontent to binary data with Coldfusion

How to create a new CouchDB User without Futon or Curl?

I'm searching for a way to create a new CouchDB user without using Futon or Curl... just a straight http request.
One way I found (http://stackoverflow.com/questions/3456256/error-creating-user-in-couchdb-1-0) puts a JSON doc to "http://localhost:5984/_users/org.couchdb.user:username" to create a user.
I have attempted the following:
<cfhttp url="http://127.0.0.1/_users/org.couchdb.user:xyz_company" port="5984" method="PUT" username="#variables.couch_username#" password="#variables.couch_password#">
<cfhttpparam type="header" name="Content-Type" value="application/json">
<cfhttpparam type='body' name='org.couchdb.user:xyz_company' value='{"roles":[],"name":"xyz_company","salt":"3B33BF09-26B9-D60A-8F469D01286E9590","id":"org.couchdb.user:xyz_company","password_sha":"096EA41A5A81EA1507F2C6F7EDC364C0B82694AC","type":"user"}'>
I keep receiving the following back from Couch:
cfhttp.statuscode = 405 Method Not Allowed
cfhttp.filecontent = Method Not Allowed; The requested method PUT is not allowed for the URL /_users/org.couchdb.user:xyz_company
Any thoughts or suggestions?
UPDATE:
I edited my code based on Marcello's suggestions. I still receive the same 405 Method Not Allowed error. Here is the code now:
<cfhttp url="http://127.0.0.1/_users/org.couchdb.user:xyz_company" port="5984" method="PUT" username="#variables.couch_username#" password="#variables.couch_password#"><cfhttpparam type="header" name="Content-Type" value="application/json;charset=UTF-8"><cfhttpparam type='body' value='{"roles":[],"name":"xyz_company","salt":"3B33BF09-26B9-D60A-8F469D01286E9590","_id":"org.couchdb.user:xyz_company","password_sha":"096EA41A5A81EA1507F2C6F7EDC364C0B82694AC","type":"user"}'></cfhttp>
Any more suggestions? Thank you!
curl is a straight http request. There are other ways to create such requests: you can craft them with your browser; you can use a different program (e.g. wget); or even write your own (e.g. in Python or in JavaScript with V8 or Rhino).