HttpRequest Class
Use the HttpRequest class to programmatically create HTTP requests like GET, POST, PATCH, PUT, and DELETE.
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.
You can set the endpoint as a named credential URL. A named credential URL contains the scheme callout:, the name of the named credential, and an optional path. For example: callout:My_Named_Credential/some_path. A named credential specifies the URL of a callout endpoint and its required authentication parameters in one definition. Salesforce manages all authentication for Apex callouts that specify a named credential as the callout endpoint so that your code doesn’t have to. See Named Credentials as Callout Endpoints.
To compress the data you send, use setCompressed. Here my_endpoint is a placeholder value.
If a response comes back in compressed format, getBody recognizes the format, uncompresses it, and returns the uncompressed value.
See Also
The following are constructors for HttpRequest.
-
Creates a new instance of the
HttpRequestclass.
Creates a new instance of the HttpRequest class.
public HttpRequest()
The following are methods for HttpRequest. All are instance methods.
-
Retrieves the body of this request.
-
Retrieves the body of this request as a Blob.
-
Retrieves the body of this request as a DOM document.
-
If
true, the request body is compressed,falseotherwise. -
Retrieves the URL for the endpoint of the external server for this request.
-
Retrieves the contents of the request header.
-
Returns the type of method used by
HttpRequest. -
Sets the contents of the body for this request.
-
Sets the contents of the body for this request using a Blob.
-
Sets the contents of the body for this request. The contents represent a DOM document.
-
setClientCertificate(clientCert, password)
This method is deprecated. Use
setClientCertificateNameinstead. -
setClientCertificateName(certDevName)
If the external service requires a client certificate for authentication, set the certificate name.
-
If
true, the data in the body is delivered to the endpoint in the gzip compressed format. Iffalse, no compression format is used. -
Specifies the endpoint for this request.
-
Sets the contents of the request header.
-
Sets the type of method to be used for the HTTP request.
-
Sets a timeout for the request between 1 and 120,000 milliseconds. The timeout is the maximum time to wait for establishing the HTTP connection. The same timeout is used for waiting for the request to start. When the request is executing, such as retrieving or posting data, the connection is kept alive until the request finishes.
-
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
Retrieves the body of this request.
public String getBody()
Type: String
Retrieves the body of this request as a Blob.
public Blob getBodyAsBlob()
Type: Blob
Retrieves the body of this request as a DOM document.
public Dom.Document getBodyDocument()
Type: Dom.Document
Use this method as a shortcut for:
If true, the request body is compressed, false otherwise.
public Boolean getCompressed()
Type: Boolean
Retrieves the URL for the endpoint of the external server for this request.
public String getEndpoint()
Type: String
Retrieves the contents of the request header.
public String getHeader(String key)
-
key:
Type: String
Type: String
Returns the type of method used by HttpRequest.
public String getMethod()
Type: String
Examples of return values:
- DELETE
- GET
- HEAD
- PATCH
- POST
- PUT
- TRACE
Sets the contents of the body for this request.
public Void setBody(String body)
-
body:
Type: String
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.
Sets the contents of the body for this request using a Blob.
public Void setBodyAsBlob(Blob body)
-
body:
Type: Blob
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.
Sets the contents of the body for this request. The contents represent a DOM document.
public Void setBodyDocument(Dom.Document document)
-
document:
Type: Dom.Document
Type: Void
Limit: 6 MB for synchronous Apex or 12 MB for asynchronous Apex.
This method is deprecated. Use setClientCertificateName instead.
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.
If the external service requires a client certificate for authentication, set the certificate name.
public Void setClientCertificateName(String certDevName)
-
certDevName:
Type: String
Type: Void
See Using Certificates with HTTP Requests.
If true, the data in the body is delivered to the endpoint in the gzip compressed format. If false, no compression format is used.
public Void setCompressed(Boolean flag)
-
flag:
Type: Boolean
Type: Void
Specifies the endpoint for this request.
public Void setEndpoint(String endpoint)
-
endpoint:
Type: String
Possible values for the endpoint:
-
Endpoint URL
-
Named credential URL, which contains the scheme
callout, the name of the named credential, and, optionally, an appended path
-
Type: Void
See Also
Sets the contents of the request header.
public Void setHeader(String key, String value)
Type: Void
Limit 100 KB.
Sets the type of method to be used for the HTTP request.
public Void setMethod(String method)
-
method:
Type: String
Possible values for the method type include:
- DELETE
- GET
- HEAD
- PATCH
- POST
- PUT
- TRACE
Type: Void
You can also use this method to set any required options.
Sets a timeout for the request between 1 and 120,000 milliseconds. The timeout is the maximum time to wait for establishing the HTTP connection. The same timeout is used for waiting for the request to start. When the request is executing, such as retrieving or posting data, the connection is kept alive until the request finishes.
public Void setTimeout(Integer timeout)
-
timeout:
Type: Integer
Type: Void
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
public String toString()
Type: String