Newer Version Available
HttpResponse Class
Namespace
Usage
Use the XML classes or JSON Classes to parse XML or JSON content in the body of a response accessed by HttpResponse.
Example
In the following getXmlStreamReader example, content is retrieved via an HTTP callout, then the XML is parsed using the XmlStreamReader class.
1public class ReaderFromCalloutSample {
2 public void getAndParse() {
3
4 // Get the XML document from the endpoint
5 Http http = new Http();
6 HttpRequest req = new HttpRequest();
7 req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/data');
8 req.setMethod('GET');
9 req.setHeader('Accept', 'application/xml');
10 HttpResponse res = http.send(req);
11
12 // Log the XML content
13 System.debug(res.getBody());
14
15 // Generate the HTTP response as an XML stream
16 XmlStreamReader reader = res.getXmlStreamReader();
17
18 // Read through the XML
19 while(reader.hasNext()) {
20 System.debug('Event Type:' + reader.getEventType());
21 if (reader.getEventType() == XmlTag.START_ELEMENT) {
22 System.debug(reader.getLocalName());
23 }
24 reader.next();
25 }
26
27 }
28}HttpResponse Methods
The following are methods for HttpResponse. All are instance methods.
getBody()
Signature
public String getBody()
Return Value
Type: String
Usage
Limit 6 MB for synchronous Apex or 12 MB for asynchronous Apex. The HTTP request and response sizes count towards the total heap size.
getBodyAsBlob()
Signature
public Blob getBodyAsBlob()
Return Value
Type: Blob
Usage
Limit 6 MB for synchronous Apex or 12 MB for asynchronous Apex. The HTTP request and response sizes count towards the total heap size.
getBodyDocument()
Signature
public Dom.Document getBodyDocument()
Return Value
Type: Dom.Document
Example
Use it as a shortcut for:
1String xml = httpResponse.getBody();
2Dom.Document domDoc = new Dom.Document(xml);getHeaderKeys()
Signature
public String[] getHeaderKeys()
Return Value
Type: String[]
getStatus()
Signature
public String getStatus()
Return Value
Type: String
getStatusCode()
Signature
public Integer getStatusCode()
Return Value
Type: Integer
getXmlStreamReader()
Signature
public XmlStreamReader getXmlStreamReader()
Return Value
Type: System.XmlStreamReader
Usage
Use it as a shortcut for:
1String xml = httpResponse.getBody();
2XmlStreamReader xsr = new XmlStreamReader(xml);setBody(body)
setBodyAsBlob(body)
setStatus(status)
Signature
public Void setStatus(String status)
Parameters
- status
- Type: String
Return Value
Type: Void
setStatusCode(statusCode)
Signature
public Void setStatusCode(Integer statusCode)
Parameters
- statusCode
- Type: Integer
Return Value
Type: Void
toString()
Signature
public String toString()
Return Value
Type: String
Example
1Status=OK, StatusCode=200