CFLib.org – Common Function Library Project

invokeRemoteMethodByJSON(url, method[, args])

Last updated March 10, 2013

author

Henry Ho

Version: 1 | Requires: CF9 | Library: UtilityLib

Description:
WSDL is heavy and slow. JSON is much faster but cfinvoke does not supporting invoking cfc method that way.

Return Values:
Any data returned by remote method call.

Example:

ans = invokeRemoteMethodByJSON("http://domain.com/remote.cfc", "sum", {arr=[1,2,3]});

Parameters:

Name Description Required
url The URL of the remote component Yes
method The name of the remote method to call Yes
args Any arguments to pass to the method No

Full UDF Source:

/**
 * Invoke a remote method using cfhttp and JSON proxy
 * v1.0 by Henry Ho
 * 
 * @param url 	 The URL of the remote component (Required)
 * @param method 	 The name of the remote method to call (Required)
 * @param args 	 Any arguments to pass to the method (Optional)
 * @return Any data returned by remote method call. 
 * @author Henry Ho (henryho167@gmail.com) 
 * @version 1.0, March 10, 2013 
 */
function invokeRemoteMethodByJSON(required string url, required string method, struct args){
	var httpService = new http(url=url, method="post");

	httpService.addParam(type="formfield", name="method", value=method);
	httpService.addParam(type="formfield", name="returnformat", value="json");
	httpService.addParam(type="formfield", name="argumentCollection", value=serializeJSON(args));
	
	var result = httpService.send().GetPrefix();
	
	if (len(result.fileContent)) {
		if (!isJson(result.fileContent)){
			throw(message="Non-json string returned", detail=result.fileContent);
		}
			
		return deserializeJSON(result.fileContent);
	}
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson