Sunday, 13 May 2018

REST API with XML and JSON | REST API with XML Payload | REST API with Dynamic Response

Recently i got one requirement to create one REST API which should support JSON and XML both. If we will pass JSON request body then response should come in JSON. But if we will send request body in XML then response should come in XML.

So here is solution :)

Global class REST_API_Helper{     public static blob formatResponse(ResponseBase ResponseBaseObj,String contentType){         String response;         String strContectType = contentType.toUpperCase();         if(strContectType.contains('XML')){             response = serializeRespinseBaseXml(ResponseBaseObj);         }             else {             response = JSON.serialize(ResponseBaseObj);         }         return blob.valueOf(response);     }     /*     *************************************************************************     * @description: This function is used to send the response in XML     * @return: string       *************************************************************************     */      public static string serializeRespinseBaseXml(ResponseBase ResponseBaseObj ){         String responseXML = '';         responseXML  += '<response>';         XmlStreamWriter w = new XmlStreamWriter();         w.writeStartElement(null , 'Message', null );         w.writeCharacters(ResponseBaseObj.Message);         w.writeEndElement();                  w.writeStartElement(null , 'Success', null );         w.writeCharacters(String.valueOf(ResponseBaseObj.Success));         w.writeEndElement();         responseXML += '</response>';         String xmlOutput = '<?xml version="1.0" encoding="UTF-8"?>' + '<Response>' + w.getXmlString()              + '</Response>';         w.close();         return xmlOutput ;     }     global class ResponseBase {         global Boolean Success {get;set;}         global String Message  {get;set;}         global ResponseBase(){                 success = true;                 message = 'Records are updated';         }     } }

global with sharing class RESTAPI_JSON_XML 
    global static void doPost(ContactDetail  Detail) {
        RestResponse standardResp = RestContext.response;
        REST_API_Helper.ResponseBase  CustomeResponse = new REST_API_Helper.ResponseBase(); 
        ContactDetail reqBodyObj = Detail; 

        RestRequest req = RestContext.request;       
        String ContentType = RestContext.request.headers.get('Content-Type') ;
        Contact cont = new Contact();
        cont.FirstName = reqBodyObj.FirstName.trim();   
        cont.LastName  = reqBodyObj.LastName.trim();
        insert cont;
        CustomeResponse.Success = true;
        CustomeResponse.Message = 'Contact Created id =';
        standardResp.statusCode = 200;
        standardResp.responseBody = REST_API_Helper.formatResponse(CustomeResponse,ContentType); 

    global class ContactDetail{
       global string FirstName {get;set;}
       global string LastName  {get;set;}
       public ContactDetail()  {}

Sample JSON Example:-
Method:- Post
URL:- /services/apexrest/api/*/createContact
Request Body
   "Detail": {
                "LastName" :"Chaudhary"

Sample XML Example:-
Method:- Post
URL:- /services/apexrest/api/*/createContact
Request Headers:-
Content-Type: application/xml; charset=UTF-8
Accept: application/xml
Request Body
<?xml version="1.0" encoding="UTF-8"?>

Response :-
<?xml version="1.0" encoding="UTF-8"?><Response><Message>Contact Created id =0039000002SMSFJAA5</Message><Success>true</Success></Response>


  1. Also did you use POSTMAN to send Request? What will be the complete url? The one I mentioned below?

  2. Where do we write the above code? Are these apex classes? Also does the user making this API request need some license/profile in Salesforce?