Use the HttpRequest class to programmatically create HTTP requests like GET, POST, PUT, and DELETE.
Use the DOM Classes or Json Classes to Parse XML or JSON content in the body of a request created by HttpRequest.
Use the DOM Classes or Json Classes to Parse XML or JSON content in the body of a request created by HttpRequest.
Methods:
The HttpRequest class contains the following public methods:
Name | Arguments | Return Type | Description |
---|---|---|---|
getBody | String | Retrieves the body of this request. | |
getBodyAsBlob | Blob | Retrieves the body of this request as a Blob. | |
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);
| |
getCompressed | Boolean | If true, the request body is compressed, false otherwise. | |
getEndpoint | String | Retrieves the URL for the endpoint of the external server for this request. | |
getHeader | String key | String | Retrieves the contents of the request header. |
getMethod | String | Returns the type of method used by HttpRequest. For example:
| |
setBody | String body | Void | Sets the contents of the body for this request. Limit: 3 MB.
The HTTP request and response sizes count towards the total heap size.
|
setBodyAsBlob | Blob body | Void | Sets the contents of the body for this request using a Blob. Limit: 3 MB.
The HTTP request and response sizes count towards the total heap size.
|
setBodyDocument | Dom.Document document | Void | Sets the contents of the body for this request. The contents represent a DOM document. Limit: 3 MB. |
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. |
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. |
setEndpoint | String endpoint | Void | Sets the URL for the endpoint of the external server for this request. |
setHeader | String key
String Value
| Void | Sets the contents of the request header. Limit 100 KB. |
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.
| |
setTimeout | Integer timeout | Void | Sets the timeout in milliseconds for the request. This can be any value between 1 and 120,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://YourServer, Method=POST |
The following example illustrates how you can use an authorization header with a request, and handle the response:
Example:
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()); } }
Compression
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');
No comments:
Post a Comment