CFFundamentals: Mimicking a form submission using CFHTTP
Note: This blog post is from 2006. Some content may be outdated--though not necessarily. Same with links and subsequent comments from myself or others. Corrections are welcome, in the comments. And I may revise the content as necessary.While developers who've used CFML for a long time will regard some topics as old hat, we have to remember that there are folks who either have come along recently or simply never used some feature. This is one of those topics, and since I answered it on a list, I offer it here.
If the form requires a login, does it do it by redirecting to a login page? If so, then you need to CFHTTP to that login page instead. If that sets a session variable, then the CFHTTP needs to capture the cookie sent back to the client which is used as the key to the session, and then it needs to pass that cookie on a subsequent page visit (after the login).
As I mentioned, this is where using a proxy/sniffer can be useful, to watch the kind of back and forth from the server to the browser to see what happens. Hope that's helpful.
I use cfhttp to make a search to a web site and i'm getting back the filecontent allright, but how can i get the url of the result page?
For example:
i use the cfhttp page to make a search like this http://www.site.com/...=john doe
And the form submit's to a page like:
http://www.site.com/...
how can i get this ^^^ url?
So, you want to view the source of the search form page (use "view source" in your browser, as I'd mentioned above). Identify what the form's ACTION attribute is, and you would use that as the URL for the CFHTTP, passing in the form fields with any values to simulate the form passing them in (using cfhttpparam type="FORMFIELD" as I show above). And again if the form ACTION you find passes in any querystring (as shown above with nav=TrackByEmail), then you pass that in cfhttpparam type="URL".
You mention passing in "john doe" (though you seem to have left off the query string variable name for it). If doing that would show the form with that "john doe" value filled in, then again you don't want to CFHTTP to it. You want to view the source of that form to see what URL the form itself calls, and CFHTTP to that.
Now, you show that the result page you'd expect to see would have a URL like http://www.site.com/... I don't think that's the URL you want to use for the CFHTTP. In fact, I'm betting that the form's action page does its lookup (based on what's passed in) and then redirects (does a CFLOCATION) to that page (having found the "id" based on the input search criteria). If that's the case, you still want to do everything I say above, but add REDIRECT="yes" to the CFHTTP, so that CF will follow the redirection from the action page to this results page. I'm just guessing here.
Hopefully it will now make sense.
What i'm really trying to do is the following:
I'm using a form so the user can search by giving a search term, like "Small Barnacled Clam", then i use cfhttp to send the term to wowhead.com to search for this term.
In fact i'm doing something similar to this:
http://www.wowhead.c...?search=Small Barnacled Clam
If you follow the above link you will see that when wowhead finds the term, it redirects to another page placing the id (item) of the item to the url: http://www.wowhead.c.../?item=5523" target="_blank">http://www.wowhead.c...
That's what i'm trying and need to grab... the item number or the full URL. Is it possible to get this url? (even if the target cfhttp page redirected)
As I alluded to above, the REDIRECT attribtue of CFLOCATION is the ticket to dealing with issues of redirection in pages you're requesting. Since you just want the URL that it WOULD redirect to, you would use REDIRECT="NO", then inspect the resulting CFHTTP struct to see what it showed. It turns out that in CF 6, 7, and 8 (at least) you will find the page they're redirecting to in the cfhttp.responseheaders structure as a key named location. The following code shows how you could see this. The first cfdump dumps the entire cfhttp struct, and the second shows you the value of the location field specifically. You'd use THAT for whatever it is you're wanting to do.
<cfhttp url="http://www.wowhead.c...?search=Small Barnacled Clam" redirect="No" >
<cfdump var="#cfhttp#">
<cfdump var="#cfhttp.responseheader.location#">
If I've helped you make some money with this, I would point you to my Amazon wish list button located on the right of the screen here. :-)
i'm gonna try it right now.
And LOL man, no it's just a test for my guild site :) It could be great if we could make some earn some from that tip! :p I need it to!