Copy link to clipboard
Copied
Hi there,
I recently built a ColdFusion 2021 server and I am trying to run "legacy code" on it.
I have a service called getBank which I could call from the browser with https://***/rest/services/getBanl.xml or /getBank.json with ColdFusion 11 which would return a response in the correct format. I am now unable to use getBank.json with the new version of ColdFusion. Please assist. See the service below:
<cfcomponent rest="true" restpath="getBanks">
<cfset datasource = "dsn">
<cffunction name="list_type_function" returntype="query">
<cfquery name="resultQuery" datasource="#datasource#">
SELECT bank_id, bank_code, bank_name
FROM banks
</cfquery>
<cfreturn resultQuery>
</cffunction>
<!--- application/json for content negotiation --->
<cffunction access="remote" name="getBanksJSON" output="false" httpmethod="GET" returntype="query" produces="application/json">
<cfset resultQuery = list_type_function()>
<cfreturn resultQuery>
</cffunction>
<!--- application/xml for content negotiation --->
<cffunction access="remote" name="getBanksXML" output="false" httpmethod="GET" returnFormat="plain" returntype="xml" produces="application/xml">
<cfset resultQuery = list_type_function()>
<!--- List of list types based on specified type code --->
<cfxml variable="XMLEval">
<banks>
<cfoutput query="resultQuery">
<cfset request.bank_code = Replace(bank_code,"&","&","ALL")>
<cfset request.bank_name = Replace(bank_name,"&","&","ALL")>
<banks_detail>
<banks_id>#bank_id#</banks_id>
<bank_code>#Trim(request.bank_code)#</bank_code>
<bank_name>#Trim(request.bank_name)#</bank_name>
</banks_detail>
</cfoutput>
</banks>
</cfxml>
<cfset XMLString = toString(XMLEval)>
<cfreturn XMLEval>
</cffunction>
</cfcomponent>
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi @BKBK, thank you for your response. I added returnFormat="json" to the getBankJSON function and it is still returning xml. returnFormat="plain" causes the following error: The value of the RETURNFORMAT attribute is invalid
Copy link to clipboard
Copied
General suggestion: to prevent the component from accidentally using the same value of variables.resultQuery throughout, replace each line
<cfset resultQuery = list_type_function()>
with
<cfset var resultQuery = list_type_function()>
That effectively makes every resultQuery function-local.
I added returnFormat="json" to the getBankJSON function and it is still returning xml.
By @KARMA28699455uxq6
Follow above suggestion, then delete 'returnType="query"', replacing it with 'returnFormat="json"'.
returnFormat="plain" causes the following error: The value of the RETURNFORMAT attribute is invalid
By @KARMA28699455uxq6
Follow the first suggestion, then replace
<cfreturn XMLEval>
with
<cfreturn XMLString>
Copy link to clipboard
Copied
Hi @BKBK ,
I have made these changes but same result. I was able to call the service using getBanks.xml and getBank.json with oulder versions but something has changed between CF11 and CF2021. I have resorted to writting a separate service until I have found a solution.