How to create a ColdFusion REST end-point which accepts query parameters

298 views Asked by At

Here is the code snippet I have tried to create a REST end point with query parameters,

<cfcomponent rest="true" restpath="api"> 
   <cffunction name="getUsersQuery" restpath="Users?filter={query}" access="remote" returntype="struct" httpmethod="GET" produces="application/json">
      <cfargument name="query" type="any" required="yes" restargsource="query" />
    
      <cfset var response = {} />
      <cfset response["message"] = "Test" />
      <cfreturn response>
  </cffunction>
</cfcomponent>

But when I tried to call the end point like below it gives "Method Not Allowed"

http://localhost:8010/rest/v1/api/Users?filter=userName eq "test"

Any help would be greatly appreciated.

1

There are 1 answers

0
rrk On BEST ANSWER

You don't need to provide ?filter={query} in the rest path since you have mentioned restargsource="query" in the argument. Also the argument name should be the URL key value.

<cffunction name="getUsersQuery" restpath="Users" access="remote" returntype="struct" httpmethod="GET" produces="application/json">
   <cfargument name="filter" type="any" required="yes" restargsource="query" />
    
   <cfset var response = {} />
   <cfset response["message"] = "Test" />
   <cfreturn response>
</cffunction>