• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Unable to switch between xml and json with rest service

Explorer ,
Feb 20, 2024 Feb 20, 2024

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,"&","&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>

 

Views

204

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 22, 2024 Feb 22, 2024

Copy link to clipboard

Copied

Use cffunction's returnFormat attribute

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 22, 2024 Feb 22, 2024

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 23, 2024 Feb 23, 2024

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.

 

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>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Feb 26, 2024 Feb 26, 2024

Copy link to clipboard

Copied

LATEST

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation