I am writing the following function in CFScript and I want to determine the requested ReturnFormat, and return the data in the appropriate format. Note that I have not defined the ReturnFormat in the function at all - I am relying on setting it in my call.
For example, the URL to call this function would be similar to:
http://localhost/com/Calendar.cfc?method=getCalendars&UserName=demo&returnFormat=json
/**
*
* @hint Returns All Calendar records for one user.
* @output false
*/
remote any function GetCalendars(required string Username) {
var data = [];
var success = false;
var message = "";
try {
query = new Query();
query.setDataSource(APPLICATION.DSN);
query.addParam(name = "username", value = Username, cfsqltype = "varchar");
query.setSQL("
SELECT idn, CalendarName, CalendarURL, CalendarColor
FROM Calendars
WHERE Username = :username
ORDER BY CalendarName, idn
");
result = query.Execute();
rs = result.getResult();
success = true;
message = "Success";
records = rs.RecordCount;
}
catch (any excpt) {
success = false;
message = "An error occurred while getting calendars for user: " & Username;
}
finally {
//TODO: If ReturnFormat = json, return a JSON string
//TODO: If ReturnFormat = wddx, returna WDDX object
//TODO: If ReturnFormat = plain, return an XML string
return rs;
}
} //end GetCalendars
Right now, this method will return either a ColdFusion automatically formatted JSON string like this:
{"COLUMNS":["IDN","CALENDARNAME","CALENDARURL","CALENDARCOLOR"],"DATA":[[1,"Demo Calendar 1","http:\/\/localhost\/calendar\/feeds\/demo1\/basic","#43cd80"],[2,"Demo Calendar 2","http:\/\/localhost\/calendar\/feeds\/demo2\/basic","#9a9cff"]]}
OR a WDDX object like this:
<wddxPacket version='1.0'><header/><data><recordset rowCount='2' fieldNames='IDN,CALENDARNAME,CALENDARURL,CALENDARCOLOR' type='coldfusion.sql.QueryTable'><field name='IDN'><number>1.0</number><number>2.0</number></field><field name='CALENDARNAME'><string>Demo Calendar 1</string><string>Demo Calendar 2</string></field><field name='CALENDARURL'><string>http:\/\/localhost\/calendar\/feeds\/demo1\/basic</string><string>http:\/\/localhost\/calendar\/feeds\/demo2\/basic</string></field><field name='CALENDARCOLOR'><string>#43cd80</string><string>#9a9cff</string></field></recordset></data></wddxPacket>
But it fails with "Invalid return type" error when I set returnFormat=plain
.
Basically I need to have a way to test for the ReturnFormat. Then I can write my own return subroutines to return JSON data formatted the way I want (lower case names anyone! - which I know how to do BTW, that's not part of this question) and in XML format.
You do not detect or do anything with returnFormat. That isn't the point. returnFormat tells CF how it should wrap your results. Repeat: You do not worry about this. Period.
So give a CFC method that makes an array, you would just return the array. CF, if is sees returnformat=json, will handle converting it to JSON.
If it sees returnformat=plain, it will thrown an error (since an array can't be a plain string).
Does this make sense?
Oh - so I see your last paragraph. If you want to try to do your own return, you should not rely on returnformat. That's baked into CF. I'd build my api to just ALWAYS return JSON, period, and do your own formatting. If you set returnFOrmat=plain on the method, it tells CF to not do anything. As long as you return a string, then you will be ok.