+ Start a Discussion
kpnkpn 

JSON / XML Response in REST API

@RestResource(urlMapping='/v1/organizations')
global with sharing class OrganizationInfoClass {

	@HttpGet
    global static void doGet() {
    	RestResponse response = RestContext.response;

 		List<Organization> OrgList = [SELECT Id, Name, IsSandbox  FROM Organization limit 1];
 		response.statusCode = 200;
 		response.responseBody = Blob.valueOf(JSON.serialize(OrgList));
    }    
}

I wanted to get the response as XML? How should I do this?
 
@RestResource(urlMapping='/v1/organizations')
global with sharing class OrganizationInfoClass {

	@HttpGet
    global static List<Organization> doGet() {
    	RestResponse response = RestContext.response;

 		List<Organization> OrgList = [SELECT Id, Name, IsSandbox  FROM Organization limit 1];
 		return OrgList;
    }    
}

In the above snippet, the return type is List<sObject> and when I do the API request with .json at the end, I'm getting JSON response and if with .xml at the end, then I'm getting the XML response.

i.e., /services/apexrest/v1/organizations.json - Gives JSON response, 
/services/apexrest/v1/organizations.xml - gives XML response.

Why the same is not working in the first snippet (where the return type is Void and setting the response to the responseBody)?
Best Answer chosen by kpn
Chris  ByromChris Byrom
Ah, I see. So in the first one you are specifically setting the body to a JSON string. That is what JSON.serialize does to your list of objects. Setting the type won't automatically convert it to XML. I would either do what you are doing in the second example, or figure out a way to build XML from your object list. As far as I know there is not a native serializer for XML like there is for JSON, but there are lots of examples of how to do it if you have to around the web. Here is one example.

http://salesforce.stackexchange.com/questions/88771/what-xml-support-salesforce-provides-in-apex

All Answers

Chris  ByromChris Byrom
You aren't returning anything in the first one. The return type is void. The second example has return type of List<Organization>. 
kpnkpn
Yes, I'm mapping the result to the response body, that's the reason the return type is void? So in this case, is it possible to get the response in XML format?

Also, if you just take a look into this https://developer.salesforce.com/forums/?id=906F00000009AtFIAU they are setting the response Content-type, but this is not working as well.. Am not sure what I'm doing wrong.. Or Am I trying something that is not possible?
Chris  ByromChris Byrom
Ah, I see. So in the first one you are specifically setting the body to a JSON string. That is what JSON.serialize does to your list of objects. Setting the type won't automatically convert it to XML. I would either do what you are doing in the second example, or figure out a way to build XML from your object list. As far as I know there is not a native serializer for XML like there is for JSON, but there are lots of examples of how to do it if you have to around the web. Here is one example.

http://salesforce.stackexchange.com/questions/88771/what-xml-support-salesforce-provides-in-apex
This was selected as the best answer
kpnkpn
That's perfect! Thanks for your time