3

I am using a wrapper class as parameter in REST API HttpPost method, my wrapper class just parse the xml document passed in parameters. What is my problem that if a person build wrong XML schema and send it in parameter in my REST API Post method, the parser class gives the error of "XML_PARSER_ERROR" in json format. Is it possible to catch the "XML_PARSER_ERROR" in my REST API POST Method and re-generate the error in XML format? Or Is it possible me to show the "XML_PARSER_ERROR" in XML format instead of JSON format?

I also try Content-Type as application/xml, still I am getting XML_PARSER_ERROR message in JSON format.

The given code sample working great if passed xml string is in correct format, given issue arrive only if the passed xml is not in desired format. Code Sample:

REST API Class Method:

@HttpPost
global static void doPost(LstBGSUpdateParamscl BGSCriminalUpdate) {
 string sResOutPut = '<?xml version=\'1.0\' encoding=\'UTF-8\'?><response><message>';
 string sReqStatus = '<status>Failed</status>';
 Savepoint sp = Database.setSavepoint();
  try {
    for(BGSUpdateParamscl oBGSUpdateParams: BGSCriminalUpdate.row) {
      if(oBGSUpdateParams.ResearcherName != null && oBGSUpdateParams.ResearcherName != '') {
      //--------Process
      }
    }
  }
  catch(exception ex){
    sResOutPut +='Operation Failed, '+ ex.getmessage();
    sReqStatus = '<status>Failed</status>';
    Database.rollback(sp);
  }
  sResOutPut += '</message>'+ sReqStatus +'</response>';
  RestContext.response.responseBody = Blob.valueOf(sResOutPut);
}

global with sharing class LstBGSUpdateParamscl {
  public BGSUpdateParamscl[] row {get; set;}
}

global with sharing class BGSUpdateParamscl {
  public string Status {get; set;}
  public string SearchID {get; set;}
  public string SearchType {get; set;}
  public string ResearcherName {get; set;}
  public string TransactionID {get; set;}
}

XML which will be passed in post method:




   
      Received
      123456
      County
      Saam
      TR123456
   
   
      Complete
      123457
      County
      Abbas
      TR123456
   


Example of calling the post method:

public void CallBGSUpdate(string xmlStr) {
    HttpRequest req = new HttpRequest();
    Http http = new Http();
    req.setMethod('POST');
    req.setHeader('Authorization', 'OAuth ' + userInfo.getSessionId());
    req.setHeader('Content-Type', 'application/xml'); 
    req.setEndpoint('https://mysfurl.com/services/apexrest/BGSUpdate');

    try {
        req.setBody(xmlStr);
        req.setCompressed(true); 
        HTTPResponse res = http.send(req);  
        sMessage = res.getBody();
    }
    catch(Exception e){
        sMessage = e.getMessage();
    }
}

Please suggest me the way.

Thanks in Advance.

  • Can you post some sample code please, I am struggling to understand your question. – Andrew Fawcett Aug 6 '13 at 7:11
  • Hello Andrew, I have added the code sample, please have a look on them. – Saam Aug 23 '13 at 6:40
2

The Content-Type HTTP header specifies what kind of content you're sending to the API. You need to set the Accept header to application/xml to specify that you want to receive an XML response:

req.setHeader('Accept', 'application/xml'); 

Alternatively, you can append .xml to the URL - e.g.

req.setEndpoint('https://mysfurl.com/services/apexrest/BGSUpdate.xml');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.