---
schema_version: "1"
id: f28c680c11c42942318f9ae60c242286a684848c3bd7b3526fb444c516c9fd15
title: HttpResponse Class
collection: legacydeveloper
version: current
locale: en-us
portal_url: https://docs.salesforce.com/legacydeveloper/current/en-us/apexref/apex_classes_restful_http_httpresponse.md
canonical_url: https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm
source_site: developer.salesforce.com
content_type: documentation
content_hash: 8485f3ee0f2622747f20bec44368a68a349e77e86ec8c2e0ddb96d3273ffb6cc
description: "Use the HttpResponse class to handle the HTTP response returned by the Http class.Retrieves the body returned in the response.Retrieves the body returned in the response as a Blob.Retrieves the body returned in the response as a DOM document.Retrieves the contents of the response header.Retrieves an array of header keys returned in the response.Retrieves the status message returned for the response.Retrieves the value of the status code returned in the response.Returns an XmlStreamReader that parses the body of the callout response.Specifies the body returned in the response.Specifies the body returned in the response using a Blob.Specifies the contents of the response header.Specifies the status message returned in the response.Specifies the value of the status code returned in the response.Returns the status message and status code returned in the response, for example:"
---
# HttpResponse Class

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

## Namespace

[System](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_namespace_System.htm "The System namespace provides classes and methods for core Apex functionality.")

## 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.

public with sharing class ReaderFromCalloutSample {
    public void getAndParse() {
 
        // Get the XML document from the endpoint
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(URL.getOrgDomainUrl().toExternalForm() + '/services/data');
        req.setMethod('GET');
        req.setHeader('Accept', 'application/xml');
        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();
        }
 
  }
}

#### See Also

- [_Apex Developer Guide_: JSON Support](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_methods_system_json_overview.htm "Apex Developer Guide: JSON Support - HTML (New Window)")
- [_Apex Developer Guide_: XML Support](https://developer.salesforce.com/docs/atlas.en-us.264.0.apexcode.meta/apexcode/apex_xml_support.htm "Apex Developer Guide: XML Support - HTML (New Window)")

## HttpResponse Methods

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

- **[getBody()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_getBody)**  
    Retrieves the body returned in the response.
- **[getBodyAsBlob()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_getBodyAsBlob)**  
    Retrieves the body returned in the response as a Blob.
- **[getBodyDocument()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_getBodyDocument)**  
    Retrieves the body returned in the response as a DOM document.
- **[getHeader(key)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_getHeader)**  
    Retrieves the contents of the response header.
- **[getHeaderKeys()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_getHeaderKeys)**  
    Retrieves an array of header keys returned in the response.
- **[getStatus()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_getStatus)**  
    Retrieves the status message returned for the response.
- **[getStatusCode()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_getStatusCode)**  
    Retrieves the value of the status code returned in the response.
- **[getXmlStreamReader()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_getXmlStreamReader)**  
    Returns an XmlStreamReader that parses the body of the callout response.
- **[setBody(body)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_setBody)**  
    Specifies the body returned in the response.
- **[setBodyAsBlob(body)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_setBodyAsBlob)**  
    Specifies the body returned in the response using a Blob.
- **[setHeader(key, value)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_setHeader)**  
    Specifies the contents of the response header.
- **[setStatus(status)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_setStatus)**  
    Specifies the status message returned in the response.
- **[setStatusCode(statusCode)](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_setStatusCode)**  
    Specifies the value of the status code returned in the response.
- **[toString()](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_restful_http_httpresponse.htm#apex_System_HttpResponse_toString)**  
    Returns the status message and status code returned in the response, for example:

### getBody()

Retrieves the body returned in the response.

#### Signature

public String getBody()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### 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()

Retrieves the body returned in the response as a Blob.

#### Signature

public Blob getBodyAsBlob()

#### Return Value

Type: [Blob](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_blob.htm#apex_methods_system_blob "Contains methods for the Blob primitive data type.")

#### 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()

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(key)

Retrieves the contents of the response header.

#### Signature

public String getHeader(String key)

#### Parameters

- **key**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

### getHeaderKeys()

Retrieves an array of header keys returned in the response.

#### Signature

public String\[\] getHeaderKeys()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")\[\]

### getStatus()

Retrieves the status message returned for the response.

#### Signature

public String getStatus()

#### Return Value

Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

### getStatusCode()

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

#### Signature

public Integer getStatusCode()

#### Return Value

Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.")

### getXmlStreamReader()

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

#### Signature

public XmlStreamReader getXmlStreamReader()

#### Return Value

Type: [System.XmlStreamReader](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_classes_xml_XmlStream_reader.htm#apex_classes_xml_XmlStream_reader "The XmlStreamReader class provides methods for forward, read-only access to XML data. You can pull data from XML or skip unwanted events. You can parse nested XML content that’s up to 50 nodes deep.")

#### Usage

Use it as a shortcut for:

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

### setBody(body)

Specifies the body returned in the response.

#### Signature

public Void setBody(String body)

#### Parameters

- **body**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: Void

### setBodyAsBlob(body)

Specifies the body returned in the response using a Blob.

#### Signature

public Void setBodyAsBlob(Blob body)

#### Parameters

- **body**: Type: [Blob](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_blob.htm#apex_methods_system_blob "Contains methods for the Blob primitive data type.")

#### Return Value

Type: Void

### setHeader(key, value)

Specifies the contents of the response header.

#### Signature

public Void setHeader(String key, String value)

#### Parameters

- **key**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")
- **value**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: Void

### setStatus(status)

Specifies the status message returned in the response.

#### Signature

public Void setStatus(String status)

#### Parameters

- **status**: Type: [String](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Return Value

Type: Void

### setStatusCode(statusCode)

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

#### Signature

public Void setStatusCode(Integer statusCode)

#### Parameters

- **statusCode**: Type: [Integer](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_integer.htm#apex_methods_system_integer "Contains methods for the Integer primitive data type.")

#### 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](https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_string.htm#apex_methods_system_string "Contains methods for the String primitive data type.")

#### Example

Status=OK, StatusCode=200