Use the XML classes or JSON classes to parse XML or JSON content in the body of a request created by HttpRequest.
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.
The following are methods for HttpRequest. All are instance methods.
public Blob getBodyAsBlob()
Type: Blob
public Dom.Document getBodyDocument()
Type: Dom.Document
Use this method as a shortcut for:
String xml = httpRequest.getBody(); Dom.Document domDoc = new Dom.Document(xml);
public Boolean getCompressed()
Type: Boolean
public String getEndpoint()
Type: String
public String getMethod()
Type: String
Examples of return values:
public Void setBody(String body)
Type: Void
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
The HTTP request and response sizes count towards the total heap size.
public Void setBodyAsBlob(Blob body)
Type: Void
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
The HTTP request and response sizes count towards the total heap size.
public Void setBodyDocument(Dom.Document document)
Type: Void
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
public Void setClientCertificate(String clientCert, String password)
Type: Void
If the server requires a client certificate for authentication, set the client certificate PKCS12 key store and password.
public Void setClientCertificateName(String certDevName)
Type: Void
public Void setCompressed(Boolean flag)
Type: Void
public Void setEndpoint(String endpoint)
https://my_endpoint.example.com/some_path
callout:My_Named_Credential/some_path
Type: Void
A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. Salesforce manages all the authentication for Apex callouts that specify a named credential as the callout endpoint, so that your code doesn’t have to. You can also skip remote site settings, which are otherwise required for Apex callouts to external sites.
By separating the endpoint URL and authentication from the Apex code, named credentials make callouts easier to maintain. For example, if an endpoint URL changes, you simply update the named credential. All callouts that reference the named credential then continue to work without any changes to the code. If you have multiple organizations, you can create a named credential with the same name in each organization. Each of these named credentials can have a different endpoint URL, for example, to accommodate differences in development and production environments. Because the code references only the named credential’s name, you can package and deploy the same Apex class in all your organizations without programmatically checking the environment.
req.setEndpoint('callout:.__My_Named_Credential/some_path');
HttpRequest req = new HttpRequest(); req.setEndpoint('callout:My_Named_Credential/some_path'); req.setMethod('GET'); Http http = new Http(); HTTPResponse res = http.send(req); System.debug(res.getBody());
HttpRequest req = new HttpRequest(); req.setEndpoint('https://my_endpoint.example.com/some_path'); req.setMethod('GET'); // Because we didn't set the endpoint as a named credential, // our code has to specify: // - The required username and password to access the endpoint // - 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());
public Void setMethod(String method)
Type: Void
You can also use this method to set any required options.
public Void setTimeout(Integer timeout)
Type: Void
The timeout can be any value between 1 and 120,000 milliseconds.
public String toString()
Type: String