HttpRequest Class

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

System

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 HttpRequest class.

public HttpRequest()

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

  • getBody()

    Retrieves the body of this request.

  • getBodyAsBlob()

    Retrieves the body of this request as a Blob.

  • getBodyDocument()

    Retrieves the body of this request as a DOM document.

  • getCompressed()

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

  • getEndpoint()

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

  • getHeader(key)

    Retrieves the contents of the request header.

  • getMethod()

    Returns the type of method used by HttpRequest.

  • setBody(body)

    Sets the contents of the body for this request.

  • setBodyAsBlob(body)

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

  • setBodyDocument(document)

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

  • setClientCertificate(clientCert, password)

    This method is deprecated. Use setClientCertificateName instead.

  • setClientCertificateName(certDevName)

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

  • setCompressed(flag)

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

    Specifies the endpoint for this request.

  • setHeader(key, value)

    Sets the contents of the request header.

  • setMethod(method)

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

  • setTimeout(timeout)

    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.

  • 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

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)

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)

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)

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)

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)

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)

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)

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