[SalesForce] How to send Post request to Rest API with XML data

I have created a rest API(apex class) in Salesforce. I have multiple nested tags in xml that comes via API request. I am wondering how do I define my post method in apex that could handle the request. Do I have to list all the fields as parameters in the method or is there a way to use DOMElement as a parameter while defining the method and then I go about extracting the data out of it.

XML Data via API:

<TestContact xmlns:xsd="http://www.w3.org/2001/XMLSchema” 
xmlns:xsl="http://www.w3.org/2001/XMLSchema-instance"> 

<VendorData> 
  <CompanyAffiliation>Test</CompanyAffiliation >  *MUST HAVE *
  <Vendor>Test Vendor</Vendor>  * MUST HAVE *
  <VendorID>xyz1234567</VendorID> * MUST HAVE * 
</VendorData>

<Contact>
  <FirstName>TestFirstName</FirstName> * MUST HAVE *  Lead
  <LastName>TestLastName</LastName> * MUST HAVE * Lead

</contact>



</TestContact> 

Rest API in Salesforce:(Approach 1)

@RestResource(urlMapping='/api/*/createContact')
global with sharing class RESTAPI_JSON_XML 
{
    @HttpPost 
    // Is this the right way to define input parameters??

    global static void doPost(String CompanyAffiliation, String Vendor, String VendorId, String FirstName, String Lastname  ) {

    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') ;

    // I don't know how to parse it and put it into appropriate objects

    Vendor__c vendor= new Vendor__c();
    vendor.Company_Affiliation__c=reqBodyObj.CompanyAffiliation;
    vendor.Vendor__c=reqBodyObj.Vendor;
    vendor.VendorID__c=reqBodyObj.VendorID__c;
    insert vendor;

    Contact con = new Contact();
    con.FirstName = reqBodyObj.FirstName.trim();   
    con.LastName  = reqBodyObj.LastName.trim();
    insert cont;

    CustomeResponse.Success = true;
    CustomeResponse.Message = 'Contact Created id ='+cont.id;
    standardResp.statusCode = 200;
    standardResp.responseBody = 
    REST_API_Helper.formatResponse(CustomeResponse,ContentType); 

   }


}

Rest API in Salesforce:(Approach 2)

@RestResource(urlMapping='/api/*/createContact')
global with sharing class RESTAPI_JSON_XML 
{
    @HttpPost 
    // Is there a way to define one input parameter that captures all the incoming information and then extract it in the body??

    global static void doPost(XMLDOMElement Detail  ) {

     ........=   Detail.CompanyAffiliation;
     ........=   Detail.FirstName;

    }

}

What is the best approach to define the post method from the above two approaches and how can I parse the information using the suggested approach(any parsing/data extracting methods)? Any sample code?

Best Answer

Do I have to list all the fields as parameters in the method or is there a way to use DOMElement as a parameter while defining the method and then I go about extracting the data out of it.

One Approach is like declare your http post method with no parameters and when you pass the XML request body it will be copied in the RestRequest.requestBody as Blob.Then you can convert to string and pass it Dom.Document.load() something like below and parse it.

  @RestResource(urlMapping='/api/*/createContact')
    global with sharing class RESTAPI_JSON_XML{
      @httppost
        global static void doPost(){
            RestRequest req = RestContext.request;
            system.debug('XML String---'+req.requestBody.toString());
            DOM.Document doc=new DOM.Document();
            doc.load(req.requestBody.toString());
            DOM.XmlNode rootNode=doc.getRootElement();
            //XML Parsing    
        }
     }