Created
March 25, 2014 00:36
Posting XML With ColdFusion, CFHttp, And CFHttpParam
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cfdump | |
var="#GetHttpRequestData()#" | |
label="Get Http Request Data" | |
/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- Get the URL. ---> | |
<cfset strURL = ( | |
CGI.server_name & | |
GetDirectoryFromPath( CGI.script_name ) & | |
"cfhttp_catch.cfm" | |
) /> | |
<!--- Create the XML data to post. ---> | |
<cfsavecontent variable="strXML"> | |
<transaction> | |
<type>Debit</type> | |
<cost>19.99</cost> | |
<date>06-01-2007</date> | |
<item> | |
<sku>GGW190034394</sku> | |
<name>Girls Gone Wild Vol. 13</name> | |
</item> | |
</transaction> | |
</cfsavecontent> | |
<!--- | |
Post the XML data to catch page. We are going | |
to post this value as an XML object will actually | |
just post it as an XML body. | |
---> | |
<cfhttp | |
url="#strURL#" | |
method="POST" | |
useragent="#CGI.http_user_agent#" | |
result="objGet"> | |
<!--- | |
When posting the xml data, remember to trim | |
the value so that it is valid XML. | |
---> | |
<cfhttpparam | |
type="XML" | |
value="#strXML.Trim()#" | |
/> | |
</cfhttp> | |
<!--- Output the return message. ---> | |
Message: #objGet.FileContent# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!--- | |
All data that is posted to this page will now | |
be part of the HTTP Request data structure. Get | |
a reference to this structure. | |
---> | |
<cfset objRequest = GetHttpRequestData() /> | |
<!--- | |
Our XML data will be the content of the request. | |
Let's grab it out of the request structure. When | |
we do this, trim the value to help create a | |
valid XML string. | |
---> | |
<cfset strXML = objRequest.Content.Trim() /> | |
<!--- | |
At this point, we may or may not have a valid XML | |
string. Before we try to do any parsing, let's | |
validate it. | |
---> | |
<cfif IsXml( strXML )> | |
<!--- | |
Now that we know the request was of valid | |
format, we can parse the XML document. | |
---> | |
<cfset xmlData = XmlParse( strXML ) /> | |
<!--- | |
For this demo, we are going to validate the | |
request ONLY for Credit / Debit. | |
---> | |
<cfset xmlType = XmlSearch( | |
xmlData, | |
"/transaction/type" | |
) /> | |
<!--- | |
Check to see if we found a Type node as a child | |
to the transaction root node. | |
---> | |
<cfif ArrayLen( xmlType )> | |
<!--- | |
We found the type - this request has been | |
validated. Now, we just need to return a | |
confirmation message. | |
---> | |
<cfset strResponse = ("#xmlType[ 1 ].XmlText# approved") /> | |
<cfelse> | |
<!--- We did not find a valid node. ---> | |
<cfset strResponse = "ERROR2: Missing type node" /> | |
</cfif> | |
<cfelse> | |
<!--- | |
The passed in data was not a valid XML string. | |
Store our error into our response message. | |
---> | |
<cfset strResponse = "ERROR1: Invalid request (Bad XML)" /> | |
</cfif> | |
<!--- | |
ASSERT: At this point, no matter what happend (if the | |
request was valid or invalid) we will have a message | |
in our response string. | |
---> | |
<!--- Return response string. ---> | |
<cfcontent | |
type="text/plain" | |
variable="#ToBinary( ToBase64( strResponse ))#" | |
/> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment