Skip to main content
Inspiring
February 20, 2024
Question

Unable to switch between xml and json with rest service

  • February 20, 2024
  • 1 reply
  • 494 views

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,"&","&amp;","ALL")>
                    <cfset request.bank_name  = Replace(bank_name,"&","&amp;","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>

 

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    February 22, 2024

    Use cffunction's returnFormat attribute

    (returnFormat="json" or returnFormat="plain")

     

    Inspiring
    February 22, 2024

    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

    BKBK
    Community Expert
    Community Expert
    February 23, 2024

    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.

     

    quote

     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"'.

     

    quote

    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>