Can CFHTTP accept a TARGET value? - coldfusion

I'm working on a Coldfusion page that takes information from a form and posts back to the same page. However, I have a CFHTTP URL string that I want to POST to a PHP page and I want to open in a separate page as well (still leaving the original POST page open. I thought CFHTTP would accept a "target - blank" value but it doesn't. Here's my code:
<cfhttp method="Post" url="https://www.foo.com/foocorp/restrict/fpdf/letter.php" result="adverseResponse">
<cfhttpparam type="Header" name="Accept-Encoding" value="deflate;q=0">
<cfhttpparam type="Header" name="TE" value="deflate;q=0">
<cfoutput>
<cfif isdefined( "b_firstname" )><cfhttpparam name="B_FIRSTNAME" type="formField" value="#B_FIRSTNAME#"></cfif>
<cfif isdefined( "b_lastname" )><cfhttpparam name="B_LASTNAME" type="formField" value="#B_LASTNAME#"></cfif>
<cfif isdefined( "prop_address" )><cfhttpparam name="PROP_ADDRESS" type="formField" value="#PROP_ADDRESS#"></cfif>
<cfif isdefined( "prop_city" )><cfhttpparam name="PROP_CITY" type="formField" value="#PROP_CITY#"></cfif>
<cfif isdefined( "prop_state" )><cfhttpparam name="PROP_STATE" type="formField" value="#PROP_STATE#"></cfif>
<cfif isdefined( "prop_zip" )><cfhttpparam name="PROP_ZIP" type="formField" value="#PROP_ZIP#"></cfif>
</cfoutput>
</cfhttp>
Is there another CF element I should be using instead of CFHTTP?

There's a few misconceptions here. CFHTTP does server to server communication. Whatever happens in CF stays in CF (to coin a slogan). What you are referring to with target="_blank" is something that is happening on the client - where a separate "page" is opened in a "browser". CFHTTP doesn't use a browser. It doesn't render content. It just issues HTTP requests with various headers and params and gives you the response back - all on the server within your cf code. You have to "do" something with the content to send it back to the browser.
So what you need to do is either:
Take the response from CF (adverseResponse.filecontent) and place it in your separate page using a popup or whatever. In this way you are acting as a "proxy" for the browser.
Abandon the CFHTTP route and user jquery or Javascript or whatever to impliment the behavior that posts to the PHP directly in the browser.
Hope this helps.

Related

How to get yahoo weather information using coldfusion

I am a newbie coldfusion developer.
And I want to get yahoo weather information using coldfusion.
So I made a cfm file like this.
<cfset theURL = "https://weather-ydn-yql.media.yahoo.com/forecastrss?location=sunnyvale,ca">
<cfhttp url="#theURL#" charset="utf-8" method="post">
<cfhttpparam type="formfield" name="appid" value="2a0Lkq6k">
<cfhttpparam type="formfield" name="context" value="dj0yJmk9MkxEcTlzVDNucE5hJnM9Y29uc3VtZXJzZWNyZXQmc3Y9MCZ4PWMx">
<cfhttpparam type="formfield" name="query" value="madonna">
</cfhttp>
And I got a error like this.
Your call to Yahoo Web Services returned an unexpected HTTP status of: 401
I have rich experience in laravel, django or so.
But I don't know coldfusion as well as yahoo weather api.
How can I fix this and get the result?

Load ColdFusion values from Form scope when name includes hyphen/dash

We're upgrading to Google's reCaptcha and it adds a field with name "g-recaptcha-response" to the form during submit. We them need to verify the response to Google with code like this:
<cfhttp url="https://www.google.com/recaptcha/api/siteverify" method="post" result="captchaResult">
<cfhttpparam type="formfield" name="secret" value="SECRET">
<cfhttpparam type="formfield" name="response" value="#Form.g-recaptcha-response#">
<cfhttpparam type="formfield" name="remoteip" value="#CGI.REMOTE_ADDR#">
</cfhttp>
However, the #Form.g-recaptcha-response# gives Element G is undefined in FORM.
Is there another way to reference Form scope to allow hyphens?
That will give you an error if the form variable doesn't exist. Try this instead:
<CFSET Form.Response = "">
<CFIF StructKeyExists(Form, "g-recaptcha-response")>
<CFSET Form.Response = form["g-recaptcha-response"]>
</CFIF>
I wrote a reCAPTCHA v2 UDF yesterday. I wanted to support sToken so I could use the same API key on multiple websites.
http://gist.github.com/JamoCA/c4e83ca402bd6175a1d7
Just found a solution:
<cfhttpparam type="formfield" name="response" value="#Form["g-recaptcha-response"]#">

get content sent with cfhttp post

I have a cfc that makes a call to a remote service using cfhttp. The service is returning a failure code, which means that my call to the remote service is not formatted properly. Is there any way to capture the content of a cfhttp post I'm sending? I want to capture the raw post data so I can see where my formatting problem is. Here is a sample of what my code looks like:
<cfhttp url="https://www.webservice.com" method="POST" result="httpResponse">
<cfhttpparam type="formField" name="method" value="doSomething">
<cfhttpparam type="formField" name="user" value="myUserName">
<cfhttpparam type="formField" name="password" value="myPassword">
</cfhttp>
I'd like to do something like this:
<cfset result = structNew() />
<cfset result["response"] = httpResponse />
<cfset result["sentContent"] = cfhttp.sentContent />
Is it possible to get the content of a sent cfhttp without looking at the server logs. My server is offsite and getting the logs will be a PITA.
You could create a template to which you would POST to. That template could return the result of GetHttpRequestData() function.

google analytics, hitting it from coldfusion server cfhttp

I have used this:
Generate Google Analytics events (__utm.gif requests) serverside
and this:
http://www.garyrgilbert.com/blog/index.cfm/2008/10/21/Tracking-Digital-Content
to build a cfhttp string so that when a user hits a page it calls google analytics. I'm doing it like this because the pages I'm serving are XML pages and I can't mix javascript with xml.
My problem is that google analytics is ignoring my requests. I have activated my bucket code on a normal html server, so it thinks/knows it exists, but now when i call any of my xml server pages and make the cfhttp request from coldfusion server, it doesn't get registered.
Update:
Following Sergii advice, I have done a dump to find out what the cfhttp is doing (i was previously missing a variable which was causing it to error), i am now getting a http return of 200, though analytics is not applying the request to my account.
Update the 2nd, including code:
<cfset var_utmac='UA-myUA'> <!--- enter the new urchin code --->
<cfset var_utmhn='www.myaddress.co.uk'>
<cfset var_utmn = RandRange(10000000,99999999)>
<cfset var_cookie = RandRange(10000000,99999999)>
<cfset var_random = RandRange(1000000000,2147483647)>
<cfset var_today = now()>
<cfset var_referer = #cgi.HTTP_REFERER#>
<cfset var_uservar = 'jevans'>
<cfset var_utmp= ''>
<cfset apiname = 'listings.getlistings'>
<cfhttp method="get" url="http://www.google-analytics.com/__utm.gif">
<cfhttpparam type="url" name="utmwv" value="1" />
<cfhttpparam type="url" name="utmn" value="#var_utmn#" />
<cfhttpparam type="url" name="utmsr" value="-" />
<cfhttpparam type="url" name="utmsc" value="-" />
<cfhttpparam type="url" name="utmul" value="-" />
<cfhttpparam type="url" name="utmje" value="0" />
<cfhttpparam type="url" name="utmfl" value="-" />
<cfhttpparam type="url" name="utmdt" value="#apiName#" />
<cfhttpparam type="url" name="utmhn" value="#var_utmhn#" />
<cfhttpparam type="url" name="utmr" value="#var_referer#" />
<cfhttpparam type="url" name="utmp" value="#var_utmp#" />
<cfhttpparam type="url" name="utmac" value="#var_utmac#" />
<cfhttpparam type="url" name="utmcc" value="__utma%3D#var_cookie#.#var_random#.#var_today#.#var_today#.#var_today#.2%3B%2B__utmb%3D#var_cookie#%3B%2B__utmc%3D#var_cookie#%3B%2B__utmz%3D#var_cookie#.#var_today#.2.2.utmccn%3D(direct)%7Cutmcsr%3D(direct)%7Cutmcmd%3D(none)%3B%2B__utmv%3D#var_cookie#.#var_uservar#%3B" />
</cfhttp>
any thoughts?
cheers
Looking at your code, I'm guessing that you need to replace &amp's in your code with regular & symbols. You only need to escape the ampersands to validate XML documents and such. If you send them over the URL, then they may not be recognized as separators.
I would actually construct it like so:
<cfhttp method="get" url="http://www.google-analytics.com/__utm.gif">
<cfhttpparam type="url" name="utmwv" value="5.1.2" />
<cfhttpparam type="url" name="utmn" value="#var_utmn#" />
... all your other URL variables
<cfhttp>
This will make your code a little easier to read, as well as make sure that all of your variables are sent over in the property format, without needing to concatenate a huge string.
It looks like several of your parameters should be of different types. You are sending them all as URL parameters. Should, for example, the HTTP_REFERER be sent as type="CGI".
Looking at my own GA HTTP, I see that in my URL string that I have utmr=-
But the request is also sending along a CGI header for Referer: http://12robots.com/
Maybe try adding another param with type="CGI" name="HTTP_REFERER" value="#CGI.HTTP_REFERER#"
You might want to look at how it is being done in this PHP class and see if you can adapt it to your ColdFusion code. It looks like it might be more than a few URl params that need setting. It is likely that you need to better simulate a real browser to make GA think you are a real browser.
http://code.google.com/p/serversidegoogleanalytics/
Pretty sure cfset var_today = now() is wrong. GA has no idea what a ColdFusion date/time object is

Upload google docs with coldfusion

I've built a forum-like app in ColdFusion and I want to add a feature with which the users can upload files to Google Docs using their Google accounts and then other users can edit those files.
I've been using this CFC: http://cfgoogle.riaforge.org/ to retrieve Google Docs, but the upload function is currently missing. I need the upload function for the first upload of the file and then for the second part of editing it. I hope that it makes sense.
That's what I'm asking help for. I'm not that experienced with cffunctions and I was wondering if someone could give me a hand with it.
This is what I have so far:
<cffunction name="upload" access="public" returnType="any" hint="I upload the document." output="false">
<cfargument name="myFile" type="string" required="true" hint="file to upload.">
<cfset var result = "">
<cfset var service = variables.docservice>
<cfset theUrl = "https://docs.google.com/feeds/documents/private/full HTTP/1.1">
<cfhttp url="#theURL#" method="post" result="result">
<cfhttpparam type="header" name="Authorization" value="GoogleLogin auth=#getAuth(service)#">
<cfhttpparam type="header" name="Content-Length" value="81047">
<cfhttpparam type="header" name="Content-Type" value="application/msword">
<cfhttpparam type="header" name="Slug" value="#myFile#">
</cfhttp>
<cfreturn result.filecontent>
</cffunction>
But I get the following error when I output the result:
'Invalid request URI'
If someone could help me out with this one (even just show me the way), it would be mostly appreciated.
I would try to expand cfgoogle CFC with upload method using Protocol Guide for uploading.