Use the HttpRequest class to programmatically create HTTP requests like GET, POST, PUT, and DELETE.
Use the DOM Classes to parse XML content in the body of a request created by HttpRequest.
The HttpRequest class contains the following public methods:
| Name | Arguments | Return Type | Description |
|---|---|---|---|
| getBody | String | Retrieves the body of this request. | |
| setBody | String body | Void | Sets the contents of the body for this request. Limit: 6 MB. The HTTP request and response sizes are calculated as part of the total heap size. Therefore, don't exceed the 6 MB total heap size. |
| getBodyAsBlob | Blob | Retrieves the body of this request as a Blob. | |
| setBodyAsBlob | Blob body | Void | Sets the contents of the body for this request using a Blob.
Limit: 6 MB. The HTTP request and response sizes are calculated as part of the total heap size. Therefore, don't exceed the 6 MB total heap size. |
| getBodyDocument | Dom.Document | Retrieves the body of this request as a DOM document. Use it
as a shortcut for:String xml = httpRequest.getBody();
Dom.Document domDoc = new Dom.Document(xml); |
|
| setBodyDocument | Dom.Document document | Void | Sets the contents of the body for this request. The contents represent a DOM document. Limit: 6 MB. |
| getCompressed | Boolean | If true, the request body is compressed, false otherwise. | |
| setCompressed | Boolean flag | Void | If true, the data in the body is delivered to the endpoint in the gzip compressed format. If false, no compression format is used. |
| getEndpoint | String | Retrieves the URL for the endpoint of the external server for this request. | |
| setEndpoint | String endpoint | Void | Sets the URL for the endpoint of the external server for this request. |
| getHeader | String key | String | Retrieves the contents of the request header. |
| setHeader | String key String Value |
Void | Sets the contents of the request header. Limit 100 KB. |
| getMethod | String | Returns the type of method used by HttpRequest. For example:
|
|
| setMethod | String method | Sets the type of method to be used for the HTTP request. For
example:
You can also use this method to set any required options. |
|
| setClientCertificate | String clientCert String password |
Void | This method is deprecated. Use setClientCertificateName instead. If the server requires a client certificate for authentication, set the client certificate PKCS12 key store and password. |
| setClientCertificateName | String certDevName | Void | If the external service requires a client certificate for authentication, set the certificate name. See Using Certificates with HTTP Requests. |
| setTimeout | Integer timeout | Void | Sets the timeout in milliseconds for the request. This can be any value between 1 and 60,000 milliseconds. |
| toString | String | Returns a string containing the URL for the endpoint of the external server for this request and the method used, for example :Endpoint=http://www.salesforcesampletest.org, Method=POST |
The following example illustrates how you can use an authorization header with a request, and handle the response:
public class AuthCallout { public void basicAuthCallout(){ HttpRequest req = new HttpRequest(); req.setEndpoint('http://www.yahoo.com'); req.setMethod('GET'); // Specify the required user name and password to access the endpoint // As well as the header and header information String username = 'myname'; String password = 'mypwd'; Blob headerValue = Blob.valueOf(username + ':' + password); String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue); req.setHeader('Authorization', authorizationHeader); // Create a new http object to send the request object // A response object is generated as a result of the request Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody()); } }
If you need to compress the data you send, use setCompressed, as the following sample illustrates:
HttpRequest req = new HttpRequest(); req.setEndPoint('my_endpoint'); req.setCompressed(true); req.setBody('some post body');
If a response comes back in compressed format, getBody automatically recognizes the format, uncompresses it, and returns the uncompressed value.