RestRequest Class

Use the System.RestRequest class to access and pass request data in a RESTful Apex method.

System

An Apex RESTful Web service method is defined using one of the REST annotations. For more information about Apex RESTful Web service, see Exposing Apex Classes as REST Web Services.

The following example shows you how to implement the Apex REST API in Apex. This class exposes three methods that each handle a different HTTP request: GET, DELETE, and POST. You can call these annotated methods from a client by issuing HTTP requests.

The following are constructors for RestRequest.

  • RestRequest()

    Creates a new instance of the System.RestRequest class.

Creates a new instance of the System.RestRequest class.

public RestRequest()

The following are properties for RestRequest.

Although the RestRequest Map properties are read-only, their contents are read-write. To modify a RestRequest header or parameter, use the associated addHeader and addParameter methods instead of modifying the Map values directly.

  • headers

    Returns the headers that are received by the request.

  • httpMethod

    Returns one of the supported HTTP request methods.

  • params

    Returns the parameters that are received by the request.

  • remoteAddress

    Returns the IP address of the client making the request.

  • requestBody

    Returns or sets the body of the request.

  • requestURI

    Returns or sets everything after the host in the HTTP request string.

  • resourcePath

    Returns the REST resource path for the request.

Returns the headers that are received by the request.

public Map<String, String> headers {get; set;}

Type: Map<String, String>

Returns one of the supported HTTP request methods.

public String httpMethod {get; set;}

Type: String

Possible values returned:

  • DELETE
  • GET
  • HEAD
  • PATCH
  • POST
  • PUT

Returns the parameters that are received by the request.

public Map <String, String> params {get; set;}

Type: Map<String, String>

Returns the IP address of the client making the request.

public String remoteAddress {get; set;}

Type: String

Returns or sets the body of the request.

public Blob requestBody {get; set;}

Type: Blob

If the Apex method has no parameters, then Apex REST copies the HTTP request body into the RestRequest.requestBody property. If there are parameters, then Apex REST attempts to deserialize the data into those parameters and the data won’t be deserialized into the RestRequest.requestBody property.

Returns or sets everything after the host in the HTTP request string.

public String requestURI {get; set;}

Type: String

For example, if the request string is https://instance.salesforce.com/services/apexrest/Account/ then the requestURI is /Account/.

Returns the REST resource path for the request.

public String resourcePath {get; set;}

Type: String

For example, if the Apex REST class defines a urlMapping of /MyResource/*, the resourcePath property returns /services/apexrest/MyResource/*.

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

At run time, you typically don’t add a header or parameter to the RestRequest object manually because they are automatically deserialized into the corresponding properties. The addHeader and addParameter methods are intended for unit testing, so you can add header or parameter values to the RestRequest object without recreating the REST method call. Use these methods instead of calling a Map method directly.

Adds a header to the request header map in an Apex test.

public Void addHeader(String name, String value)

Type: Void

This method is intended for unit testing of Apex REST classes.

The following headers aren’t allowed:

  • cookie
  • set-cookie
  • set-cookie2
  • content-length
  • authorization

If any of these headers are used, an Apex exception is thrown.

Adds a parameter to the request params map in an Apex test.

public Void addParameter(String name, String value)

Type: Void

This method is intended for unit testing of Apex REST classes.