HttpResponse Class

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

Namespace

System

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 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(); } } }

HttpResponse Methods

The following are methods for HttpResponse. All are instance methods.

getBody()

Retrieves the body returned in the response.

Signature

public String getBody()

Return Value

Type: String

Usage

Limit3 MB. The HTTP request and response sizes count towards the total heap size.

getBodyAsBlob()

Retrieves the body returned in the response as a Blob.

Signature

public Blob getBodyAsBlob()

Return Value

Type: Blob

Usage

Limit3 MB. The HTTP request and response sizes count towards the total heap size.

getBodyDocument()

Retrieves the body returned in the response as a DOM document.

Signature

public Dom.Document getBodyDocument()

Return Value

Type: Dom.Document

Example

Use it as a shortcut for:

String xml = httpResponse.getBody();
Dom.Document domDoc = new Dom.Document(xml);

getHeader(String)

Retrieves the contents of the response header.

Signature

public String getHeader(String key)

Parameters

key
Type: String

Return Value

Type: String

getHeaderKeys()

Retrieves an array of header keys returned in the response.

Signature

public String[] getHeaderKeys()

Return Value

Type: String[]

getStatus()

Retrieves the status message returned for the response.

Signature

public String getStatus()

Return Value

Type: String

getStatusCode()

Retrieves the value of the status code returned in the response.

Signature

public Integer getStatusCode()

Return Value

Type: Integer

getXmlStreamReader()

Returns an XmlStreamReader that parses the body of the callout response.

Signature

public XmlStreamReader getXmlStreamReader()

Return Value

Type: System.XmlStreamReader

Usage

Use it as a shortcut for:

String xml = httpResponse.getBody();
XmlStreamReader xsr = new XmlStreamReader(xml);

setBody(String)

Specifies the body returned in the response.

Signature

public Void setBody(String body)

Parameters

body
Type: String

Return Value

Type: Void

setBodyAsBlob(Blob)

Specifies the body returned in the response using a Blob.

Signature

public Void setBodyAsBlob(Blob body)

Parameters

body
Type: Blob

Return Value

Type: Void

setHeader(String, String)

Specifies the contents of the response header.

Signature

public Void setHeader(String key, String value)

Parameters

key
Type: String
value
Type: String

Return Value

Type: Void

setStatus(String)

Specifies the status message returned in the response.

Signature

public Void setStatus(String status)

Parameters

status
Type: String

Return Value

Type: Void

setStatusCode(Integer)

Specifies the value of the status code returned in the response.

Signature

public Void setStatusCode(Integer statusCode)

Parameters

statusCode
Type: Integer

Return Value

Type: Void

toString()

Returns the status message and status code returned in the response, for example:

Signature

public String toString()

Return Value

Type: String

Example

Status=OK, StatusCode=200
© Copyright 2000–2014 salesforce.com, inc. All rights reserved.
Various trademarks held by their respective owners.