Newer Version Available
HttpRequest Class
Namespace
Usage
Use the XML classes or JSON classes to parse XML or JSON content in the body of a request created by HttpRequest.
Example
The following example illustrates how you can use an authorization header with a request, and handle the response:
1swfobject.registerObject("clippy.codeblock-0", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class AuthCallout {
18
19 public void basicAuthCallout(){
20 HttpRequest req = new HttpRequest();
21 req.setEndpoint('http://www.yahoo.com');
22 req.setMethod('GET');
23
24 // Specify the required user name and password to access the endpoint
25 // As well as the header and header information
26
27 String username = 'myname';
28 String password = 'mypwd';
29
30 Blob headerValue = Blob.valueOf(username + ':' + password);
31 String authorizationHeader = 'BASIC ' +
32 EncodingUtil.base64Encode(headerValue);
33 req.setHeader('Authorization', authorizationHeader);
34
35 // Create a new http object to send the request object
36 // A response object is generated as a result of the request
37
38 Http http = new Http();
39 HTTPResponse res = http.send(req);
40 System.debug(res.getBody());
41 }
42}Compression
If you need to compress the data you send, use setCompressed, as the following sample illustrates:
1HttpRequest req = new HttpRequest();
2req.setEndPoint('my_endpoint');
3req.setCompressed(true);
4req.setBody('some post body');
5If a response comes back in compressed format, getBody automatically recognizes the format, uncompresses it, and returns the uncompressed value.
HttpRequest Methods
The following are methods for HttpRequest. All are instance methods.
getBodyAsBlob()
Signature
public Blob getBodyAsBlob()
Return Value
Type: Blob
getBodyDocument()
Signature
public Dom.Document getBodyDocument()
Return Value
Type: Dom.Document
Example
Use this method as a shortcut for:
1String xml = httpRequest.getBody();
2Dom.Document domDoc = new Dom.Document(xml);getCompressed()
Signature
public Boolean getCompressed()
Return Value
Type: Boolean
getEndpoint()
Signature
public String getEndpoint()
Return Value
Type: String
getMethod()
Signature
public String getMethod()
Return Value
Type: String
Usage
Examples of return values:
- DELETE
- GET
- HEAD
- POST
- PUT
- TRACE
setBody(body)
Signature
public Void setBody(String body)
Parameters
- body
- Type: String
Return Value
Type: Void
Usage
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
The HTTP request and response sizes count towards the total heap size.
setBodyAsBlob(body)
Signature
public Void setBodyAsBlob(Blob body)
Parameters
- body
- Type: Blob
Return Value
Type: Void
Usage
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
The HTTP request and response sizes count towards the total heap size.
setBodyDocument(document)
Signature
public Void setBodyDocument(Dom.Document document)
Parameters
- document
- Type: Dom.Document
Return Value
Type: Void
Usage
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
setClientCertificate(clientCert, password)
Signature
public Void setClientCertificate(String clientCert, String password)
Return Value
Type: Void
Usage
If the server requires a client certificate for authentication, set the client certificate PKCS12 key store and password.
setClientCertificateName(certDevName)
Signature
public Void setClientCertificateName(String certDevName)
Parameters
- certDevName
- Type: String
Return Value
Type: Void
Usage
setCompressed(flag)
Signature
public Void setCompressed(Boolean flag)
Parameters
- flag
- Type: Boolean
Return Value
Type: Void
setEndpoint(endpoint)
Signature
public Void setEndpoint(String endpoint)
Parameters
- endpoint
- Type: String
- Possible values for the endpoint:
- Endpoint
URL
1https://my_endpoint.example.com/some_path - Named credential URL, which contains the scheme callout, the name of the named credential, and, optionally, an appended
path
1callout:My_Named_Credential/some_path
- Endpoint
URL
Return Value
Type: Void
Usage with Named Credentials
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.
1req.setEndpoint('callout:.__My_Named_Credential/some_path');Example
1swfobject.registerObject("clippy.codeblock-6", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17HttpRequest req = new HttpRequest();
18req.setEndpoint('callout:My_Named_Credential/some_path');
19req.setMethod('GET');
20Http http = new Http();
21HTTPResponse res = http.send(req);
22System.debug(res.getBody());
23
You can code the callout endpoint as the URL
instead of the named credential, but your code then handles the authentication. Our
example uses basic password authentication, but keep in mind that OAuth authentication
is much more complex and best handled with named
credentials.1swfobject.registerObject("clippy.codeblock-7", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17HttpRequest req = new HttpRequest();
18req.setEndpoint('https://my_endpoint.example.com/some_path');
19req.setMethod('GET');
20
21// Because we didn't set the endpoint as a named credential,
22// our code has to specify:
23// - The required username and password to access the endpoint
24// - The header and header information
25
26String username = 'myname';
27String password = 'mypwd';
28
29Blob headerValue = Blob.valueOf(username + ':' + password);
30String authorizationHeader = 'BASIC ' +
31EncodingUtil.base64Encode(headerValue);
32req.setHeader('Authorization', authorizationHeader);
33
34// Create a new http object to send the request object
35// A response object is generated as a result of the request
36
37Http http = new Http();
38HTTPResponse res = http.send(req);
39System.debug(res.getBody());
40setMethod(method)
Signature
public Void setMethod(String method)
Parameters
- method
- Type: String
- Possible values for the method type include:
- DELETE
- GET
- HEAD
- POST
- PUT
- TRACE
Return Value
Type: Void
Usage
You can also use this method to set any required options.
setTimeout(timeout)
Signature
public Void setTimeout(Integer timeout)
Parameters
- timeout
- Type: Integer
Return Value
Type: Void
Usage
The timeout can be any value between 1 and 120,000 milliseconds.
toString()
Signature
public String toString()
Return Value
Type: String