Newer Version Available

This content describes an older version of this product. View Latest

HttpRequest Class

Use the HttpRequest class to programmatically create HTTP requests like GET, POST, PUT, and DELETE.

Namespace

System

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");public class AuthCallout {
2 
3   public void basicAuthCallout(){
4     HttpRequest req = new HttpRequest();
5     req.setEndpoint('http://www.yahoo.com');
6     req.setMethod('GET');
7     
8     // Specify the required user name and password to access the endpoint
9     // As well as the header and header information
10 
11     String username = 'myname';
12     String password = 'mypwd';
13  
14     Blob headerValue = Blob.valueOf(username + ':' + password);
15     String authorizationHeader = 'BASIC ' +
16     EncodingUtil.base64Encode(headerValue);
17     req.setHeader('Authorization', authorizationHeader);
18   
19     // Create a new http object to send the request object
20     // A response object is generated as a result of the request  
21  
22     Http http = new Http();
23     HTTPResponse res = http.send(req);
24     System.debug(res.getBody());
25   }
26}

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');
5

If a response comes back in compressed format, getBody automatically recognizes the format, uncompresses it, and returns the uncompressed value.

HttpRequest Constructors

The following are constructors for HttpRequest.

HttpRequest()

Creates a new instance of the HttpRequest class.

Signature

public HttpRequest()

HttpRequest Methods

The following are methods for HttpRequest. All are instance methods.

getBody()

Retrieves the body of this request.

Signature

public String getBody()

Return Value

Type: String

getBodyAsBlob()

Retrieves the body of this request as a Blob.

Signature

public Blob getBodyAsBlob()

Return Value

Type: Blob

getBodyDocument()

Retrieves the body of this request as a DOM document.

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()

If true, the request body is compressed, false otherwise.

Signature

public Boolean getCompressed()

Return Value

Type: Boolean

getEndpoint()

Retrieves the URL for the endpoint of the external server for this request.

Signature

public String getEndpoint()

Return Value

Type: String

getHeader(String)

Retrieves the contents of the request header.

Signature

public String getHeader(String key)

Parameters

key
Type: String

Return Value

Type: String

getMethod()

Returns the type of method used by HttpRequest.

Signature

public String getMethod()

Return Value

Type: String

Usage

Examples of return values:

  • DELETE
  • GET
  • HEAD
  • POST
  • PUT
  • TRACE

setBody(String)

Sets the contents of the body for this request.

Signature

public Void setBody(String body)

Parameters

body
Type: String

Return Value

Type: Void

Usage

Limit: 3 MB.

The HTTP request and response sizes count towards the total heap size.

setBodyAsBlob(Blob)

Sets the contents of the body for this request using a Blob.

Signature

public Void setBodyAsBlob(Blob body)

Parameters

body
Type: Blob

Return Value

Type: Void

Usage

Limit: 3 MB.

The HTTP request and response sizes count towards the total heap size.

setBodyDocument(Dom.Document)

Sets the contents of the body for this request. The contents represent a DOM document.

Signature

public Void setBodyDocument(Dom.Document document)

Parameters

document
Type: Dom.Document

Return Value

Type: Void

Usage

Limit: 3 MB.

setClientCertificate(String, String)

This method is deprecated. Use setClientCertificateName instead.

Signature

public Void setClientCertificate(String clientCert, String password)

Parameters

clientCert
Type: String
password
Type: String

Return Value

Type: Void

Usage

If the server requires a client certificate for authentication, set the client certificate PKCS12 key store and password.

setClientCertificateName(String)

If the external service requires a client certificate for authentication, set the certificate name.

Signature

public Void setClientCertificateName(String certDevName)

Parameters

certDevName
Type: String

Return Value

Type: Void

setCompressed(Boolean)

If true, the data in the body is delivered to the endpoint in the gzip compressed format. If false, no compression format is used.

Signature

public Void setCompressed(Boolean flag)

Parameters

flag
Type: Boolean

Return Value

Type: Void

setEndpoint(String)

Sets the URL for the endpoint of the external server for this request.

Signature

public Void setEndpoint(String endpoint)

Parameters

endpoint
Type: String

Return Value

Type: Void

setHeader(String, String)

Sets the contents of the request header.

Signature

public Void setHeader(String key, String Value)

Parameters

key
Type: String
Value
Type: String

Return Value

Type: Void

Usage

Limit 100 KB.

setMethod(String)

Sets the type of method to be used for the HTTP request.

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(Integer)

Sets the timeout in milliseconds for the request.

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()

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

Signature

public String toString()

Return Value

Type: String