Skip to main content
Participant
March 17, 2021
Answered

CFC defined in {} shorthand behaving weirdly

  • March 17, 2021
  • 1 reply
  • 329 views

Weird behavior of cfc defined using shorthand:

component {remote function get() {return "helllooooo";}}

 

compared to normal behavior defined this way:

<cfcomponent>
<cffunction name="get" access="remote" returntype="String" httpmethod="GET">
<cfreturn "hello world">
</cffunction>
</cfcomponent>

 

The 2nd example called as a REST service correctly returns hello world

The 1st example however returns helllooooo but ALSO appends the entire component's .cfc code as follows:

 

helllooooocomponent { remote string function get() { return "helllooooo"; } }

 

Any ideas anybody what could be causing this? I'm stumped. Thks

    This topic has been closed for replies.
    Correct answer BKBK

    Wow. Thanks for the detailed answer.

    It appears to be an issue with my Application.cfc

     

    <cffunction name="onRequestStart">
    <!---Handle OnRequestStart Callback--->
    <cfargument type="string" name="TargetPage" required=true />
    <cfinclude template="#arguments.TargetPage#" />
    <cfreturn true />
    </cffunction>

     

    when I removed <cfinclude template="#arguments.TargetPage#" />

    from OnRequestStart and moved to OnRequest method it seems to be a fix?


    <cffunction name="onRequest">
    <!---Handle OnRequest Callback--->
    <cfargument name = "targetPage" type="String" required=true/> </cffunction>

    <cfinclude template="#arguments.TargetPage#" />


    That explains it. It is in fact the reason I asked about your ColdFusion version. 

     

    In recent ColdFusion versions  you shouldn't be using either onRequestStart or onRequest to handle a REST request. If necessary, use instead the onRESTRequest handler in Application.cfc:

    public any function onRESTRequest(required string cfcname, required string methodName, required struct args)
    {
        // Invoke the REST application having given CFCName, methodName and arguments
        var result = invoke(arguments.cfcname, arguments.methodName, arguments.args);
        return result;
    }

     

    1 reply

    Participant
    March 18, 2021

    Hi, following on from this I updated the shorthand component definition as follows:

    component {
    remote string function get() httpmethod="GET" returnformat="plain" {
    return "helllooooo";
    }
    }

     

    Same result:

    helllooooocomponent { remote string function get() httpmethod="GET" returnformat="plain" { return "helllooooo"; } }

     

    Any ideas anyone?

    BKBK
    Community Expert
    Community Expert
    March 18, 2021

    What is your ColdFusion version and update level? Are you sure you set REST up correctly?

     

    I am on ColdFusion 2021. When I ran your cfscript code I got the expected result. 

     

    You can test my code yourself. Create a directory and place the following code in it:

     

    Application.cfc

    component {
        this.name="TestRestApp";
        this.applicationTimeout = "#createTimespan(1,0,0,0)#";
        this.sessionManagement = "true";
        this.sessionTimeout = "#createTimeSpan(0,0,20,0)#";
    
        // Configure ColdFusion to find REST CFCs in the subdirectory cfcDir (within the current directory)
        this.restsettings.cfclocation = "./cfcDir";
        this.restsettings.skipcfcwitherror = false;
    	 
        public boolean function onApplicationStart()
        {   
            return true;
        }
         
        public boolean function onRequestStart()
        {   
            // Initialize REST, giving it the same name as the application. That is, "TestRestApp".
        	restInitApplication(getDirectoryFromPath(getCurrentTemplatePath()), this.name);
            return true;
        }
    }
    

     

    cfcDir / TestRest.cfc

    // The REST CFC, located in the subdirectory cfcDir (within the current directory)
    component rest="true" restpath="/" {
        remote function get() returntype="string" httpmethod="GET" {
    	return "helllooooo";
        }
    }

     

    testPage.cfm

    // The call to the REST service
    <cfscript>
    cfhttp(method="GET", url="http://localhost:8500/rest/TestRestApp", result="res") {
    }
    writeoutput("Test result: " & res.filecontent);
    </cfscript>
    

     

     

     

    Participant
    March 19, 2021

    Wow. Thanks for the detailed answer.

    It appears to be an issue with my Application.cfc

     

    <cffunction name="onRequestStart">
    <!---Handle OnRequestStart Callback--->
    <cfargument type="string" name="TargetPage" required=true />
    <cfinclude template="#arguments.TargetPage#" />
    <cfreturn true />
    </cffunction>

     

    when I removed <cfinclude template="#arguments.TargetPage#" />

    from OnRequestStart and moved to OnRequest method it seems to be a fix?


    <cffunction name="onRequest">
    <!---Handle OnRequest Callback--->
    <cfargument name = "targetPage" type="String" required=true/> </cffunction>

    <cfinclude template="#arguments.TargetPage#" />