HttpResponse Class

Use the HttpResponse class to handle the HTTP response returned by the Http class.

Use the DOM Classes to parse XML content in the body of a response accessed by HttpResponse.

The HttpResponse class contains the following public methods:

Name Arguments Return Type Description
getBody String Retrieves the body returned in the response. Limit6 MB.

The HTTP request and response sizes are calculated as part of the total heap size. Therefore, don't exceed the 6 MB total heap size.

getBodyAsBlob Blob Retrieves the body returned in the response as a Blob. Limit6 MB.

The HTTP request and response sizes are calculated as part of the total heap size. Therefore, don't exceed the 6 MB total heap size.

getBodyDocument Dom.Document Retrieves the body returned in the response as a DOM document. Use it as a shortcut for:
String xml = httpResponse.getBody();
Dom.Document domDoc = new Dom.Document(xml);
getHeader String key String Retrieves the contents of the response header.
getHeaderKeys String[] Retrieves an array of header keys returned in the response.
getStatus String Retrieves the status message returned for the response.
getStatusCode Integer Retrieves the value of the status code returned in the response.
getXmlStreamReader XmlStreamReader Returns an XmlStreamReader (XmlStreamReader Class) that parses the body of the callout response. Use it as a shortcut for:
String xml = httpResponse.getBody();
XmlStreamReader xsr = new XmlStreamReader(xml);

For a full example, see getXmlStreamReader example.

toString String Returns the status message and status code returned in the response, for example:
Status=OK, StatusCode=200

In the following getXmlStreamReader example, content is retrieved from an external Web server, then the XML is parsed using the XmlStreamReader class.

public class ReaderFromCalloutSample {

  public void getAndParse() {

    // Get the XML document from the external server 
    
    Http http = new Http();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://www.cheenath.com/tutorial/sample1/build.xml');
    req.setMethod('GET');
    HttpResponse res = http.send(req);

    // Log the XML content 
    
    System.debug(res.getBody());

    // Generate the HTTP response as an XML stream 
    
    XmlStreamReader reader = res.getXmlStreamReader();

    // Read through the XML 
    
    while(reader.hasNext()) {
      System.debug('Event Type:' + reader.getEventType());
      if (reader.getEventType() == XmlTag.START_ELEMENT) {
        System.debug(reader.getLocalName());
      }
      reader.next();
    }
 
  }
}
© Copyright 2000–2012 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.