CFC defined in {} shorthand behaving weirdly

Community Beginner ,
Mar 17, 2021 Mar 17, 2021

Copy link to clipboard

Copied

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

Views

85

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct Answers

Community Beginner , Mar 19, 2021 Mar 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--->...

Likes

Translate

Translate
Adobe Community Professional , Mar 19, 2021 Mar 19, 2021
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.c...

Likes

Translate

Translate
Community Beginner ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 18, 2021 Mar 18, 2021

Copy link to clipboard

Copied

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>

 

 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

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#" />

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
Mar 19, 2021 Mar 19, 2021

Copy link to clipboard

Copied

LATEST

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;
}

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines