Wednesday, September 18, 2013

HttpResponse Class in Salesforce for Handling HTTP Response returned by HTTP Class

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

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


Methods in HTTPResponse Class: 

The HttpResponse class contains the following public methods:
NameArgumentsReturn TypeDescription
getBodyStringRetrieves the body returned in the response. Limit3 MB.
The HTTP request and response sizes count towards the total heap size.
getBodyAsBlobBlobRetrieves the body returned in the response as a Blob. Limit3 MB.
The HTTP request and response sizes count towards the total heap size.
getBodyDocumentDom.DocumentRetrieves 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);
getHeaderString keyStringRetrieves the contents of the response header.
getHeaderKeysString[]Retrieves an array of header keys returned in the response.
getStatusStringRetrieves the status message returned for the response.
getStatusCodeIntegerRetrieves the value of the status code returned in the response.
getXmlStreamReaderXmlStreamReaderReturns 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 below Example.
setBodyStringVoidSpecifies the body returned in the response.
setBodyAsBlobBlobVoidSpecifies the body returned in the response using a Blob.
setHeaderString key, String valueVoidSpecifies the contents of the response header.
setStatusStringVoidSpecifies the status message returned in the response.
setStatusCodeIntegerVoidSpecifies the value of the status code returned in the response.
toStringStringReturns the status message and status code returned in the response, for example:
Status=OK, StatusCode=200

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

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

Enjoy........Keep Smiling.....

No comments:

Post a Comment