+ Start a Discussion

HTTP callouts and parsing server HTML response


I have written HTTP callout which calls external service like that:

  Http h = new Http(); 
        String strURL = BASE_URL +
        '?user=' + USER_NAME + 
        '&pwd=' + PWD;
        HTTPRequest req = new HttpRequest(); 

 Then response is checked for server errors and for callout aplication errors.

        try {
            HttpResponse res = h.send(req); 
            Integer statusCode = res.getStatusCode();
            String statusName = res.getStatus();
                // Check for returned code. 200 means OK. 
                if (StatusCode == 200) {
                    /* Parse error message returned by external app */
                    String message_type = ''; 
String message_description = '';
String message_key = '';
String message_ref = '';

/* Code which process HTML response should go here */
errorMsg = 'Connected with server. Response: ' + 'Message type: ' + message_type; ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, errorMsg)); } // Dsiplay error Msg if other status code is returned. else { errorMsg = 'Request failed. Please contact administrator. <br/> Error Code: <b>' + statusCode + '</b><br/> Error Message: <b>' + statusName + '</b>'; ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING, errorMsg)); } } catch (System.CalloutException e) { System.debug('Callout ERROR: '+ e); }



 Sample HTML response from external service which contains information about error:

<?xml version="1.0" encoding="UTF-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<span id="message_type" >ERROR</span>
<span id="message_description" >You are not allowed to use this function! </span>
<span id="message_key" >UNAUTHORIZED</span>
<span id="message_ref" >1341329156068</span>


Question is: How to extract informatins from HTML to their APEX varaiables?


<span id="message_type" >ERROR</span>  content should be assigned to   String message_type = ''; 

after processing  message_type should has value 'ERROR'


I have read about:

HttpResponse Class http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_restful_http_httpresponse.htm#httpresponse_class_name

XmlNode Class http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_dom_xmlnode.htm

Document Class http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_xml_dom_document.htm

XmlStreamReader Class http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_XmlStream_reader.htm

but still not able to do anything usefull with that. 

Best Answer chosen by Admin (Salesforce Developers) 

An html response will have to be parsed just like any string.


Can you have the service return json or xml data back instead?

All Answers


An html response will have to be parsed just like any string.


Can you have the service return json or xml data back instead?

This was selected as the best answer

Ok. Then I will do that in this way.

I was thinkging that maybe there is better solution for that.


Unfortunetally service can not be changed I need to fit to it.