RestResponse Class

Represents an object used to pass data from an Apex RESTful Web service method to an HTTP response.

System

Use the System.RestResponse class to pass response data from an Apex RESTful web service method that is defined using one of the REST annotations.

The following are constructors for RestResponse.

  • RestResponse()

    Creates a new instance of the System.RestResponse class.

Creates a new instance of the System.RestResponse class.

public RestResponse()

The following are properties for RestResponse.

Although the RestResponse Map properties are read-only, their contents are read-write. To modify a RestResponse header, use the associated addHeader method instead of the modifying the Map values directly.

  • responseBody

    Returns or sets the body of the response.

  • headers

    Returns the headers to be sent to the response.

  • statusCode

    Returns or sets the response status code.

Returns or sets the body of the response.

public Blob responseBody {get; set;}

Type: Blob

The response is either the serialized form of the method return value or it’s the value of the responseBody property based on the following rules:

  • If the method returns void, then Apex REST returns the response in the responseBody property.
  • If the method returns a value, then Apex REST serializes the return value as the response. If the return value contains fields with null value, those fields are not serialized in the response.

Returns the headers to be sent to the response.

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

Type: Map<String, String>

Returns or sets the response status code.

public Integer statuscode {get; set;}

Type: Integer

The following are valid response status codes. The status code is returned by the RestResponse.statusCode property.

If you set the RestResponse.statusCode property to a value that’s not listed in the table, then an HTTP status of 500 is returned with the error message “Invalid status code for HTTP response: nnn” where nnn is the invalid status code value.

Status CodeDescription
200OK
201CREATED
202ACCEPTED
204NO_CONTENT
206PARTIAL_CONTENT
300MULTIPLE_CHOICES
301MOVED_PERMANENTLY
302FOUND
304NOT_MODIFIED
400BAD_REQUEST
401UNAUTHORIZED
403FORBIDDEN
404NOT_FOUND
405METHOD_NOT_ALLOWED
406NOT_ACCEPTABLE
409CONFLICT
410GONE
412PRECONDITION_FAILED
413REQUEST_ENTITY_TOO_LARGE
414REQUEST_URI_TOO_LARGE
415UNSUPPORTED_MEDIA_TYPE
417EXPECTATION_FAILED
500INTERNAL_SERVER_ERROR
503SERVER_UNAVAILABLE

The following are instance methods for RestResponse.

Adds a header to the response header map.

public Void addHeader(String name, String value)

Type: Void

The following headers aren’t allowed:

  • cookie
  • set-cookie
  • set-cookie2
  • content-length
  • authorization
  • Header names that aren’t RFC 7230 compliant

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