Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
An example of writing REST web services in ColdFusion, without a framework. This code is provided as an example of a simple implementation, but should not be considered a best practice or something I am promoting. I use this in one of my presentations as
An example of writing REST web services in ColdFusion, without a framework (and why you should use one).
Provided as an example of a simple implementation, but should not be considered a best practice or something
I am promoting. Not thoroughly tested, not very maintainable, not even that reusable.
<cfsetting enablecfoutputonly="true" showdebugoutput="false" />
<cffunction name="StructKeyRequire" output="false">
<cfargument name="s" type="struct" />
<cfargument name="k" type="string" />
<cfset var local = {} />
<cfloop list="#arguments.k#" index="local.i">
<cfif not structKeyExists(s, local.i)>
<cfthrow type="rest.invalidRequest" message="Required field `#local.i#` is missing from your request. Required fields for this action: `#arguments.k#`" />
<!--- get verb --->
<cfset verb = cgi.request_method />
<cfset httpMethodOverride = GetPageContext().getRequest().getHeader("X-HTTP-Method-Override") />
<cfif isDefined("httpMethodOverride")>
<cfset verb = httpMethodOverride />
<!--- get url params --->
<cfset params = {} />
<cfset structAppend(params,url,true) />
<!--- get request body --->
<cfset requestBody = getHTTPRequestData().content />
<cfif len(requestBody)>
<cfset args = listToArray(requestBody,"&") />
<cfloop from="1" to="#arrayLen(args)#" index="arg">
<cfif findNoCase("=",args[arg])>
<cfset params[listGetAt(args[arg],1,'=')] = listRest(args[arg],'=') />
<!--- get headers --->
<cfset headers = getHTTPRequestData().headers />
<!--- get ID --->
<cfif len(cgi.path_info)>
<cfset = listGetAt(cgi.path_info,1,'/') />
<!--- check for api key --->
<cfif not structKeyExists(params, "apiKey")>
<cfthrow type="rest.unauthorized" />
<!--- do stuff --->
<cfswitch expression="#verb#">
<cfcase value="get">
<cfquery name="data" datasource="cfartgallery">
select * from ARTISTS
<cfif structKeyExists(params, "id")>
where artistId = <cfqueryparam cfsqltype="cf_sql_numeric" value="" />
<cfif data.recordCount eq 0>
<cfthrow type="rest.notFound" />
<cfcase value="put">
<cfthrow type="rest.methodNotAllowed" />
<cfcase value="post">
<cfset StructKeyRequire(params, "firstname,lastname,addres,city,state,postalcode,phone,email,fax,thepassword") />
<cfquery name="doInsert" datasource="cfartgallery" result="insertResult">
values (
<cfqueryparam cfsqltype="cf_sql_varchar" value="#params.firstname#" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="#params.lastname#" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="#params.address#" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="#params.state#" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="#params.postalcode#" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="#params.fax#" />
, <cfqueryparam cfsqltype="cf_sql_varchar" value="#params.thepassword#" />
<cfheader statuscode="201" statustext="Created" />
<cfcontent reset="true" type="application/json" />
<!--- inserted id in sql server and apache derby --->
<cfif structKeyExists(insertResult, "identitycol")>
<cfheader name="x-inserted-id" value="#insertResult.identityCol#" />
<!--- inserted id in mysql --->
<cfif structKeyExists(insertResult, "generated_key")>
<cfheader name="x-inserted-id" value="#insertResult.generated_key#" />
<cfabort />
<cfcase value="delete">
<cfthrow type="rest.methodNotAllowed" />
<!--- serialize result --->
<cfset output=serializeJson(data) />
<!--- set return headers --->
<cfheader statuscode="200" statustext="OK" />
<cfcontent reset="true" type="application/json" />
<!--- output return body --->
handle potential errors
<cfcatch type="rest.invalidRequest">
<cfheader statuscode="400" statustext="Bad Request" />
<cfcontent reset="true" type="application/json">
<cfcatch type="rest.unauthorized">
<cfheader statuscode="401" statustext="Unauthorized" />
<cfcontent reset="true" type="application/json">
<cfoutput>{"errors":["API Key is required for all requests"]}</cfoutput>
<cfcatch type="rest.notFound">
<cfheader statuscode="404" statustext="Not Found" />
<cfcontent reset="true" type="application/json">
<cfoutput>{"errors":["The ID you specified was not found"]}</cfoutput>
<cfcatch type="rest.methodNotAllowed">
<cfheader statuscode="405" statustext="Method Not Allowed" />
<cfcontent reset="true" type="application/json">
<cfoutput>{"errors":["The method you used is now allowed for this resource"]}</cfoutput>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.