Created
August 1, 2011 15:57
-
-
Save bendalton/1118410 to your computer and use it in GitHub Desktop.
ColdBox JSON Interceptor to support json http requests
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* handles application/json content type requests | |
*/ | |
component { | |
void function configure(){} | |
void function preProcess(event,struct interceptData){ | |
var rc = event.getCollection(); | |
if(isJSONRequest()) | |
{ | |
var jsonString = toString(getHttpRequestData().content); | |
var jsonObject = deserializeJSON( jsonString ); | |
structAppend(rc,jsonObject,true); | |
} | |
} | |
Boolean function isJSONRequest(){ | |
var contentType = getHTTPHeader("Content-Type",""); | |
return find("application/json",contentType) != 0; | |
} | |
function getHTTPHeader(header,defaultValue){ | |
var headers = getHttpRequestData().headers; | |
if( structKeyExists(headers, arguments.header) ){ | |
return headers[arguments.header]; | |
} | |
if( structKeyExists(arguments,"defaultValue") ){ | |
return arguments.defaultValue; | |
} | |
throw(message="Header #arguments.header# not found in HTTP headers",detail="Headers found: #structKeyList(headers)#",type="RequestContext.InvalidHTTPHeader"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment