A REST (Representational State Transfer) resource is an abstraction of a piece of information or an action such as a single data record, a collection of records, or a query. Each resource in REST API is identified by a named Uniform Resource Identifier (URI) and accessed using standard HTTP methods (HEAD, GET, POST, PATCH, DELETE). REST API is based on the usage of resources, their Uniform Resource Identifiers (URIs), and the links between them.

You can use a resource to interact with your Salesforce org. For instance, you can perform actions such as:

  • Retrieve summary information about the Application programming interface (API) versions available to you.
  • Obtain detailed information about a Salesforce object, such as Account, User, or a custom object.
  • Perform a query or search.
  • Update or delete records.

A REST request consists of four components: a resource URI, an HTTP method, request headers, and a request body. Request headers specify metadata for the request. The request body specifies data for the request, when necessary. If there’s no data to specify, the body is omitted from the request.

Basically, Restful services are REST architecture based on Web Services. RESTful web services are lightweight, highly scalable, and maintainable and are used to craft APIs for web-based applications.

Benefits of Restful Web Services

Fast: RESTful Web Services are fast because there are no strict specifications like SOAP. It uses less bandwidth and resources.

Language and platform-independent: RESTful web services can be written in any programming language and run on any platform.

Can use SOAP: RESTful web services can use SOAP (Simple Object Access Protocol) web services as the implementation tools.

Allows various data formats: RESTful web service allows various data formats such as Plain text, HTML, XML, and JSON.

Apex REST Methods

Apex REST supports two formats for representations of resources: JSON and XML.

JSON representations are passed by default in the body of a request or response and the format is indicated by the Content-Type property in the HTTP header. You can retrieve the body as a Blob from the HttpRequest object if there are no parameters to the Apex method.

If parameters are defined in the Apex method, an attempt is made to deserialize the request body into those parameters. If the Apex method has a non-void return type, the resource representation is serialized into the response body.

These return and parameter types are allowed:

  • Apex primitives (excluding sObject and Blob).
  • sObjects
  • Lists or maps of Apex primitives or sObjects (only maps with String keys are supported).
  • User-defined types that contain member variables of the types listed above.

Methods annotated with @HttpGet or @HttpDelete should have no parameters. This is because GET and DELETE requests have no request body, so there’s nothing to deserialize.

A single Apex class annotated with @RestResource can’t have multiple methods annotated with the same HTTP request method. For example, the same class can’t have two methods annotated with @HttpGet.

Example:

You can use user-defined types for parameters in your Apex REST methods. Apex REST deserializes request data into public, private, or global class member variables of the user-defined type unless the variable is declared as static or transient. For example, an Apex REST method that contains a user-defined type parameter might look like the following:

@RestResource(urlMapping=’/user_defined_type_example/*’)

global with sharing class MyOwnTypeRestResource {

    @HttpPost

    global static MyUserDefinedClass echoMyType(MyUserDefinedClass ic) {

        return ic;

    }

    global class MyUserDefinedClass {

        global String string1;

        global String string2 { get; set; }

        private String privateString;

        global transient String transientString;

        global static String staticString;

    }

}

Valid JSON and XML request data for this method would look like:

{ “ic” : {

                “string1” : “value for string1”,

                “string2” : “value for string2”,

                “privateString” : “value for privateString” 

}}

Conclusion

REST stands for Representational State Transfer. REST is used to build lightweight, well-maintained and scalable web services. The main aspects of REST are the resources on the server and the verbs GET, POST, PUT, and DELETE which can be used to work with these resources. Visual Studio and .NET can be used to build Restful Web Services.

If you are looking for certified Salesforce developers or Salesforce implementation partners, Cloud Analogy — the world’s leading and most successful Salesforce Development Company — would be the right choice for you.

Pin It on Pinterest