Copy link to clipboard
Copied
I was using the event handler onRESTRequest to log any REST request in my ColdFusion 2018 application. When I decided to upgrade it to ColdFusion 2021, I've got a surprise to receive an error informing that the event handler does not exist anymore!
Could someone explain me the reason that made ColdFusion creators deciding to remove that TOO IMPORTANT function for us, CFers? Are they trying to push the new ColdFusion REST Manager resource at our throat? Could they respect website manages that host their application at a shared hosting service? What are they thinking about?
OnRESTRequest has NOT been removed. Why do you say that it has?
In any case, what follows is a REST application I have just created on ColdFusion 2021. The files share the same directory.
/* Application.cfc */
component output="false"
{
this.name = "RestTestApp";
this.applicationTimeout = createTimespan(0,1,0,0);
this.sessionTimeout = createTimespan(0,0,20,0);
this.restsettings.skipCFCWithError = true;
public boolean function onRequestStart()
{
...
Copy link to clipboard
Copied
OnRESTRequest has NOT been removed. Why do you say that it has?
In any case, what follows is a REST application I have just created on ColdFusion 2021. The files share the same directory.
/* Application.cfc */
component output="false"
{
this.name = "RestTestApp";
this.applicationTimeout = createTimespan(0,1,0,0);
this.sessionTimeout = createTimespan(0,0,20,0);
this.restsettings.skipCFCWithError = true;
public boolean function onRequestStart()
{
restInitApplication(getDirectoryFromPath(getCurrentTemplatePath()), this.name);
return true;
}
public void function onError(exception, eventName) {
// For debugging purposes
writedump(var=exception, label="Exception in OnError", format="html", output="#expandPath('RESTException.html')#");
}
public any function onRESTRequest(string cfcname, string method, struct args) {
var RESTResponse = invoke(arguments.cfcname, arguments.method , arguments.args);
// For debugging purposes
writedump(var=application, label="Application Scope dump within OnRESTRequest", format="html", output="#expandPath('OnRESTRequest_dump.html')#");
writedump(var=arguments, label="OnRESTRequest Arguments Scope", format="html", output="#expandPath('OnRESTRequest_dump.html')#");
return RESTResponse;
}
}
/* User.cfc */
/* CFC to simulate User entity. Accessors = true. Hence ColdFusion automatically generates getters and setters for each property */
component accessors="true" output="false"
{
property name="userid" type="numeric" ;
property name="username" type="string";
property name="firstname" type="string";
property name="lastname" type="string";
property name="email" type="string";
/* Simulate data for a user whose ID is 9876 */
public User function init() {
variables.userid=9876;
variables.username="jsmth";
variables.firstname="John";
variables.lastname="Smith";
variables.email="john.smith@fakenews.com";
return this;
}
}
/* RestSample.cfc */
component restpath="users" rest="true"
{
remote struct function getUser(numeric userid restargsource="Path") httpmethod="GET" restpath="{userid}"
{
var user = new User();
var response = structNew();
if(arguments.userid eq 9876)
{
response.userid = user.getUserId();
response.username = user.getUsername();
response.firstname = user.getFirstname();
response.lastname = user.getLastname();
response.email = user.getEmail();
} else {
throw(type="Restsample.UserNotFoundError", errorCode='404', detail='User not found');
}
return response;
}
}
<!--- getUserFromId.cfm: Get a user whose userID is 9876 --->
<cfhttp
url="http://localhost:8500/rest/RestTestApp/users/9876?format=json"
method="GET"
result="res">
</cfhttp>
<cfdump var = "#res#">
Copy link to clipboard
Copied
Everytime I tried to use onRESTRequest I receive an error called "NullSupport". It happens everytime a function does not exist or you do not send a mandatory parameter to an UDF. It may happen when you send an empty SQL statement to queryExecute function too.
In your example, you have used the "invoke()" that has replaced the "cfinvoke()" (and adaped statement for CFSCRIPT) at ColdFusion 2018 update 11. The "cfinvoke()" was common until update 10 and it's documented at CFDocs (https://cfdocs.org/cfinvoke). Probably the "NullSupport" error was referenced by the cfinvoke() instead of onRESTRequest.
According to your example, you did not need to use the "createObject()" function for the first parameter in invoke(). At ColdFusion 2018 update 11 the "createObject()" is mandatory to return an instantiated object, not just its path mapped by dot istead of slash. May you confirm it?
Thanks a lot for your explanation that has broken my concept!
Copy link to clipboard
Copied
Everytime I tried to use onRESTRequest I receive an error called "NullSupport". It happens everytime a function does not exist or you do not send a mandatory parameter to an UDF. It may happen when you send an empty SQL statement to queryExecute function too.
By @I.am.Sceiffer
The NullSupport error is most probably caused by something else other than onRESTRequest.
In your example, you have used the "invoke()" that has replaced the "cfinvoke()" (and adaped statement for CFSCRIPT) at ColdFusion 2018 update 11. The "cfinvoke()" was common until update 10 and it's documented at CFDocs (https://cfdocs.org/cfinvoke). Probably the "NullSupport" error was referenced by the cfinvoke() instead of onRESTRequest
Invoke() is a new function in its own right, introduced in CF 10. It is NOT the cfscript variant of the cfinvoke tag.
There is one big difference between the two. Invoke() has 3 arguments whereas cfinvoke() has 16. It is therefore more likely for cfinvoke() to reference a null object.
According to your example, you did not need to use the "createObject()" function for the first parameter in invoke(). At ColdFusion 2018 update 11 the "createObject()" is mandatory to return an instantiated object, not just its path mapped by dot istead of slash. May you confirm it?
Yes, indeed. The first argument in invoke() need not be an object or instance. It can be a string representing a path. In fact, it can even be an empty string. For example, when you're within a CFC and you invoke a method within the same CFC.