HTTP stands for Hypertext Transfer Protocol and it was first used in the World Wide Web global information initiative in 1990.
Since then, HTTP has became a part of our every application, basically we can said and a part of our lives. Every day when we search something on the internet we use HTTP , sometimes we have to actively use HTTP to solve problem.
ColdFusion has built-in its own tags for HTTP support. As web developer you have to take advantage how to use this tag.
First we are going to see how HTTP actually works.
Simplest definition is that HTTP is way for clients to request something from a Web server, but for Web servers to provide response for clients request.If you type into your browser some adress like (e.g. http://socialads.guru/ ) you actually telling your browser to send HTTP request to the web server, then server process your request, get data and returning into your browser.
ColdFusion uses his tag cfhttp for http processes.
The main functionality of cfhttp is to retrieve data from another page,site or api. First of all you have to tell which url are you gona use and which method.
<cfhttp url=”Url_which_you_need” method=”get” >
In this example we are using GET method. When cfhttp returns data it’s like a structure.
In that data we have variable FileContent and this variable holds all the data returned to us by the provided URL.
We can dump that variable to see raw text.
Sometimes you may need to pass some parameters to the URL or put value of that parameters into database using method post.
ColdFusion also have built-in tag for http parametars (cfhttpparam) and they have to be written between cfhttp tag.
Example is taken from this link
<!— This example consists of two CFML pages.
The first page posts to the second. —>
<!— The first, posting page.
This page posts variables to another page and displays the body of the response from
the second page. Change the URL and port as necessary for your environment. —>
<cfhttpparam name=”form_test” type=”FormField” value=”This is a form variable”>
<cfhttpparam name=”url_test” type=”URL” value=”This is a URL variable”>
<cfhttpparam name=”cgi_test” type=”CGI” value=”This is a CGI variable”>
<cfhttpparam name=”cookie_test” type=”Cookie” value=”This is a cookie”>
<!— Output the results returned by the posted-to page. —>
<!— This is the cfhttpparamexample.cfm page that receives and processes the Post request. Its response body is the generated HTML output. —>
<h3>Output the passed variables</h3>
Form variable: #form.form_test#
URL variable: #URL.url_test#
Cookie variable: #Cookie.cookie_test#
CGI variable: #CGI.cgi_test#
Note that the CGI variable is URL encoded.
In this example we can see that those two tags provide you a really good deal of flexibility to your application, also they can help you to manipulate over your CF scripts.
Now we can try together a short example of cfhttp.
Write cfhttp tag and forward a url,method and store result into one variable.
In the second step you have te parse/deseriazle JSON or XML (small tips: By using variable from cfhttp tag you can access fileContent)
Write a loop and print the parsed data (It would be good to do cfdump of results before printing – see picture above)
If you want a little help, see below:
<cfset parsed = deserializeJSON(JSONdata.fileContent) />
<cfloop from=”1″ to=”#arrayLen(parsed.episodes)#” index=”i”>
<!— <cfdump var=”#parsed.episodes#”/> —>
<cfoutput>Released: #parsed.episodes[i].Released# </cfoutput>
<cfoutput>imdbRating #parsed.episodes[i].imdbRating# </cfoutput>