Skip to content

Instantly share code, notes, and snippets.

@bendalton
Created August 1, 2011 15:57
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bendalton/1118410 to your computer and use it in GitHub Desktop.
Save bendalton/1118410 to your computer and use it in GitHub Desktop.
ColdBox JSON Interceptor to support json http requests
/**
* 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