Use the XML classes or JSON Classes to parse XML or JSON content in the body of a response accessed by HttpResponse.
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('https://docsample.herokuapp.com/xmlSample'); 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(); } } }
The following are methods for HttpResponse. All are instance methods.
public String getBody()
Type: String
Limit 6 MB for synchronous Apex or 12 MB for asynchronous Apex. The HTTP request and response sizes count towards the total heap size.
public Blob getBodyAsBlob()
Type: Blob
Limit 6 MB for synchronous Apex or 12 MB for asynchronous Apex. The HTTP request and response sizes count towards the total heap size.
public Dom.Document getBodyDocument()
Type: Dom.Document
Use it as a shortcut for:
String xml = httpResponse.getBody(); Dom.Document domDoc = new Dom.Document(xml);
public String[] getHeaderKeys()
Type: String[]
public String getStatus()
Type: String
public Integer getStatusCode()
Type: Integer
public XmlStreamReader getXmlStreamReader()
Type: System.XmlStreamReader
Use it as a shortcut for:
String xml = httpResponse.getBody(); XmlStreamReader xsr = new XmlStreamReader(xml);
public Void setStatus(String status)
Type: Void
public Void setStatusCode(Integer statusCode)
Type: Void
public String toString()
Type: String
Status=OK, StatusCode=200