How to call a web API using XML from ColdFusion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sj3vans
    New Member
    • May 2007
    • 2

    How to call a web API using XML from ColdFusion

    I'm trying to integrate a ColdFusion application with a webAPI provided by another company to access their services. Their requirements are that requests should be generated using standard HTTP Posts with the following HTTP Headers:
    • Content-Type: text/XML
    • Content-Length: {Length of content}


    The sample code they sent on how to access the API are .bat files that call their API via JAVA. Here is one of the .bat files:

    Code:
    @echo off
    call classpath.bat
    java WSClient http://wsdev.THEIRWEBSITE.com/services/v1.2/API.xml
    I don't see how to do this in ColdFusion. Can anyone tell me how to do this?

    I tried this ColdFusion Code that I found on the Internet and inserted sample XML they gave me:

    Code:
    <cfsetting  enablecfoutputonly="no" showdebugoutput="no" />
    
    <CFSAVECONTENT VARIABLE="strpost">
    	<?xml version="1.0" encoding="UTF-8"?>
    	<addNewRecipients xmlns ="http://www.THEIRWEBSITE.com/ws">
    		<apiVersion>2.7</apiVersion>
    		<authorization>
    			<username>MYUSERNAME</username>
    			<password>MYPASSWD</password>
    		</authorization>
    		<recipientDetail>
    			<firstName>John</firstName>
    			<lastName>Smith</lastName>
    			<devices>
    				<workPhone>
    					<address>1491830876</address>
    					<description>Work Phone number</description>
    				</workPhone>
    			</devices>
    		</recipientDetail>
    	</addNewRecipients>
    </CFSAVECONTENT>
    
    <cfoutput>
    	<CFHTTP URL="http://wsdev.THEIRWEBSITE.com/services/v1.2/"  METHOD="post" RESOLVEURL="false">
    		<CFCONTENT TYPE="text/XML"> 
    		<CFHTTPPARAM NAME="xml" TYPE="formfield" VALUE="#strpost#">
    	</CFHTTP> 
    </cfoutput>
    <!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en"
    "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
    
    <html>
        <head>
            <title>XML Demo</title>
        </head>
    
        <body>
            <CFTRY>
                <CFOUTPUT>
                    
                    <strong>received</strong> response from server: 
                    <p></p>
                    <pre>#cfhttp.header#
                    #StructKeyList(cfhttp.responseheader)#</pre>
                    
                    <form><textarea cols="70" rows="20" wrap="off">#cfhttp.filecontent#</textarea></form>
                    <p></p>
                    <hr size="1" />
                    
                </CFOUTPUT>
                
                <CFCATCH TYPE="any">
                    <CFTHROW TYPE="display" MESSAGE="can't display" DETAIL="#cfcatch.message#">
                </CFCATCH>
                
                <!--- no parse yet --->
                
                <CFCATCH TYPE="xml">
                    <CFOUTPUT> i have an xml error: <br/><code>#cfcatch.message#<br/>#cfcatch.detail#</code></CFOUTPUT>
                </CFCATCH>
                
                <CFCATCH TYPE="display">
                    <CFOUTPUT>i can't display anything.<br/><code>#cfcatch.message#<br/>#cfcatch.detail#</code></CFOUTPUT>
                </CFCATCH>
                
                <CFCATCH TYPE="any">
                    <CFOUTPUT>
                    issues with #server#<p></p>
                    <code>#cfcatch.message#<br />#cfcatch.detail#</code>
                    </CFOUTPUT>            
                </CFCATCH>
            
            </CFTRY>
            
        </body>
    </html>
    When I use it to try to send the XML, I get this error back from their servers:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
      <soapenv:Body>
        <soapenv:Fault>
          <soapenv:faultcode>soapenv:Server.userException</soapenv:faultcode>
          <soapenv:faultstring>error: Unexpected end of file after null</soapenv:faultstring>
          <soapenv:detail/>
        </soapenv:Fault>
      </soapenv:Body>
    </soapenv:Envelope>
    Any idea where to go from here? I am apparently their only customer using Coldfusion. Thanks!
    Last edited by acoder; Apr 2 '12, 12:27 PM.
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Welcome to TSDN.
    Use the cfinvoke tag, see link. Also, see this link about web services.

    Comment

    • sj3vans
      New Member
      • May 2007
      • 2

      #3
      I spent qutie a bit of time trying to get the cfinvoke tag to work but could not. It seems to need a "method" which the service does not apear to have. The XML they say to send looks like this:

      Code:
      <?xml version="1.0" encoding="UTF-8"?>
      <addNewRecipients xmlns ="http://www.THEIRWEBSITE.com/ws">
        <apiVersion>2.7</apiVersion>
        <authorization>
           <username>MYUSERNAME</username>
           <password>MYPASSWD</password>
        </authorization>
        <recipientDetail>
           <firstName>John</firstName>
           <lastName>Smith</lastName>
           <devices>
             <workPhone>
                <address>1491830876</address>
                <description>Work Phone number</description>
             </workPhone>
            </devices>
        </recipientDetail>
      </addNewRecipients>
      How do I send that?

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        OK, this is probably not like a normal web service.

        Go back to the first method. For cfhttpparam, change the the type to "XML", see link. This should set the correct mimetype headers.

        Comment

        Working...