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:
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:
Name | Arguments | Return Type | Description |
---|---|---|---|
getBody | String | Retrieves the body returned in the response. Limit3 MB.
The HTTP request and response sizes count towards the total heap size.
| |
getBodyAsBlob | Blob | Retrieves the body returned in the response as a Blob. Limit3 MB.
The HTTP request and response sizes count towards the total heap size.
| |
getBodyDocument | Dom.Document | Retrieves 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);
| |
getHeader | String key | String | Retrieves the contents of the response header. |
getHeaderKeys | String[] | Retrieves an array of header keys returned in the response. | |
getStatus | String | Retrieves the status message returned for the response. | |
getStatusCode | Integer | Retrieves the value of the status code returned in the response. | |
getXmlStreamReader | XmlStreamReader | Returns 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.
| |
setBody | String | Void | Specifies the body returned in the response. |
setBodyAsBlob | Blob | Void | Specifies the body returned in the response using a Blob. |
setHeader | String key, String value | Void | Specifies the contents of the response header. |
setStatus | String | Void | Specifies the status message returned in the response. |
setStatusCode | Integer | Void | Specifies the value of the status code returned in the response. |
toString | String | Returns 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